Java Array Coding Problems
Click on any problem to view its solution
Basic Problems
1. Find Largest and Smallest Element in an Array
Given an array, find the maximum and minimum elements.
Example: [10, 5, 20, 8] → Max = 20, Min = 5
public class MinMaxInArray {
public static void findMinMax(int[] arr) {
if (arr == null || arr.length == 0) {
System.out.println("Array is empty");
return;
}
int min = arr[0];
int max = arr[0];
for (int num : arr) {
if (num < min) min = num;
if (num > max) max = num;
}
System.out.println("Max = " + max + ", Min = " + min);
}
public static void main(String[] args) {
int[] arr = {10, 5, 20, 8};
findMinMax(arr); // Output: Max = 20, Min = 5
}
}
2. Reverse an Array
Reverse the elements of an array in place.
Example: [1, 2, 3, 4, 5] → [5, 4, 3, 2, 1]
public class ReverseArray {
public static void reverse(int[] arr) {
int start = 0;
int end = arr.length - 1;
while (start < end) {
// Swap elements
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
reverse(arr);
System.out.println(Arrays.toString(arr)); // Output: [5, 4, 3, 2, 1]
}
}
3. Find Second Largest Number in an Array
Find the second largest element in an array.
Example: [10, 20, 5, 40] → 20
public class SecondLargest {
public static int findSecondLargest(int[] arr) {
if (arr == null || arr.length < 2) {
throw new IllegalArgumentException("Array should have at least two elements");
}
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
for (int num : arr) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num != first) {
second = num;
}
}
return second;
}
public static void main(String[] args) {
int[] arr = {10, 20, 5, 40};
System.out.println(findSecondLargest(arr)); // Output: 20
}
}
4. Check if an Array is Sorted
Determine if an array is sorted in ascending order.
Example: [1, 2, 3, 4] → true, [3, 1, 2] → false
public class CheckSortedArray {
public static boolean isSorted(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {3, 1, 2};
System.out.println(isSorted(arr1)); // Output: true
System.out.println(isSorted(arr2)); // Output: false
}
}
Intermediate Problems
5. Remove Duplicates from Sorted Array
Remove duplicates from a sorted array in place.
Example: [1, 1, 2, 2, 3] → [1, 2, 3]
public class RemoveDuplicates {
public static int removeDuplicates(int[] arr) {
if (arr.length == 0) return 0;
int uniqueIndex = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] != arr[uniqueIndex]) {
uniqueIndex++;
arr[uniqueIndex] = arr[i];
}
}
return uniqueIndex + 1;
}
public static void main(String[] args) {
int[] arr = {1, 1, 2, 2, 3};
int newLength = removeDuplicates(arr);
for (int i = 0; i < newLength; i++) {
System.out.print(arr[i] + " "); // Output: 1 2 3
}
}
}
6. Move All Zeros to End of Array
Move all zeros to the end while maintaining the order of other elements.
Example: [0, 1, 0, 3, 12] → [1, 3, 12, 0, 0]
public class MoveZeros {
public static void moveZeros(int[] arr) {
int nonZeroIndex = 0;
// Move all non-zero elements to the front
for (int num : arr) {
if (num != 0) {
arr[nonZeroIndex++] = num;
}
}
// Fill remaining positions with zeros
while (nonZeroIndex < arr.length) {
arr[nonZeroIndex++] = 0;
}
}
public static void main(String[] args) {
int[] arr = {0, 1, 0, 3, 12};
moveZeros(arr);
System.out.println(Arrays.toString(arr)); // Output: [1, 3, 12, 0, 0]
}
}
Hello
ReplyDelete