HackerRank Java Problems and Solutions
■Problem 1: Odd or Even Counter
Problem Description:
Write a program that reads a list of integers and counts how many are even and how many are odd.
Input Format:
The first line contains an integer `n` (1 <= n <= 1000) - the number of integers.
The second line contains `n` space-separated integers.
Output Format:
Two integers separated by space: the count of even numbers and the count of odd numbers.
Sample Input:
5
12345
Sample Output:
23
Java Solution:
import [Link].*;
public class OddEvenCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int n = [Link]();
int even = 0, odd = 0;
for (int i = 0; i < n; i++) {
int num = [Link]();
if (num % 2 == 0)
even++;
else
odd++;
}
[Link](even + " " + odd);
}
}
------------------------------------------------------------
■Problem 2: Factorial Finder
Problem Description:
Given a non-negative integer `n`, compute its factorial.
Input Format:
A single integer `n` (0 <= n <= 20)
Output Format:
A single integer, the factorial of `n`.
Sample Input:
5
Sample Output:
120
Java Solution:
import [Link].*;
public class FactorialFinder {
public static long factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int n = [Link]();
■Problem 3: Palindrome Checker
Problem Description:
Check if the given string is a palindrome. Ignore case and spaces.
Input Format:
A single line string.
Output Format:
Print `YES` if it is a palindrome, otherwise print `NO`.
Sample Input:
Race car
Sample Output:
YES
Java Solution:
import [Link].*;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String s = [Link]().replaceAll("\\s", "").toLowerCase();
String reversed = new StringBuilder(s).reverse().toString();
if ([Link](reversed))
[Link]("YES");
else
[Link]("NO");
}
}
------------------------------------------------------------
■Problem 4: Maximum Number in Array
Problem Description:
Find the maximum number in an array.
Input Format:
First line contains an integer `n` (1 <= n <= 1000).
Second line contains `n` space-separated integers.
Output Format:
A single integer: the maximum value in the array.
Sample Input:
4
-1 20 3 5
Sample Output:
20
Java Solution:
import [Link].*;
public class MaxInArray {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int n = [Link]();
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
int num = [Link]();
if (num > max) max = num;
}
[Link](max);
}
}
------------------------------------------------------------
■Problem 5: Sum of Digits
Problem Description:
Given an integer, return the sum of its digits.
Input Format:
A single integer `n` (-10^6 <= n <= 10^6)
Output Format:
A single integer: the sum of digits.
Sample Input:
1234
Sample Output:
10
Java Solution:
import [Link].*;
public class SumOfDigits {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int n = [Link]([Link]());
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
[Link](sum);
}
}
------------------------------------------------------------