3. Move Zeros to the End of the array while maintaining the order of non-zero elements.
public class MoveZeros {
public static void moveZerosToEnd(int[] arr) {
int j = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) {
arr[j++] = arr[i];
}
}
while (j < arr.length) {
arr[j++] = 0;
}
}
public static void main(String[] args) {
int[] arr = {0, 1, 0, 3, 12};
moveZerosToEnd(arr);
System.out.println("Array after moving zeros: " + Arrays.toString(arr));
}
}
4. Rotate an Array by k Positions to the Right.
public class RotateArray {
public static void rotateArray(int[] arr, int k) {
k = k % arr.length;
reverseArray(arr, 0, arr.length - 1);
reverseArray(arr, 0, k - 1);
reverseArray(arr, k, arr.length - 1);
}
private static void reverseArray(int[] arr, int start, int end) {
while (start < end) {
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, 6, 7};
int k = 3;
rotateArray(arr, k);
System.out.println("Array after rotation: " + Arrays.toString(arr));
}
}
5. Find the Largest Sum Contiguous Subarray (Kadane’s Algorithm).
public class KadaneAlgorithm {
public static int maxSubArraySum(int[] arr) {
int maxSum = arr[0], currentSum = arr[0];
for (int i = 1; i < arr.length; i++) {
currentSum = Math.max(arr[i], currentSum + arr[i]);
maxSum = Math.max(maxSum, currentSum);
}
return maxSum;
}
public static void main(String[] args) {
int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
System.out.println("Maximum sum: " + maxSubArraySum(arr));
}
}
14. Find All Pairs in an Array that Sum Up to a Specific Target.
import java.util.*;
public class PairSum {
public static void findPairs(int[] arr, int target) {
Set<Integer> set = new HashSet<>();
for (int num : arr) {
int complement = target - num;
if (set.contains(complement)) {
System.out.println("Pair: (" + num + ", " + complement + ")");
}
set.add(num);
}
}
public static void main(String[] args) {
int[] arr = {1, 4, 6, 8, 3, 2};
int target = 10;
findPairs(arr, target);
}
}
15. Find the Minimum Product Subarray.
public class MinimumProductSubarray {
public static int minProduct(int[] arr) {
int n = arr.length;
int minProd = arr[0], maxProd = arr[0], result = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < 0) {
int temp = maxProd;
maxProd = minProd;
minProd = temp;
}
maxProd = Math.max(arr[i], maxProd * arr[i]);
minProd = Math.min(arr[i], minProd * arr[i]);
result = Math.min(result, minProd);
}
return result;
}
public static void main(String[] args) {
int[] arr = {2, 3, -2, 4};
System.out.println("Minimum product subarray: " + minProduct(arr));
}
}
16. Find the Minimum Difference Between Any Two Elements in an Array.
import java.util.*;
public class MinimumDifference {
public static int findMinDifference(int[] arr) {
Arrays.sort(arr);
int minDiff = Integer.MAX_VALUE;
for (int i = 1; i < arr.length; i++) {
minDiff = Math.min(minDiff, arr[i] - arr[i - 1]);
}
return minDiff;
}
public static void main(String[] args) {
int[] arr = {1, 5, 3, 19, 18, 25};
System.out.println("Minimum difference: " + findMinDifference(arr));
}
}
20. Find the Subarray with the Maximum Sum (Prefix Sum).
public class MaximumSumSubarray {
public static int maxSubarraySum(int[] arr) {
int maxSum = arr[0], currentSum = arr[0];
for (int i = 1; i < arr.length; i++) {
currentSum = Math.max(arr[i], currentSum + arr[i]);
maxSum = Math.max(maxSum, currentSum);
}
return maxSum;
}
public static void main(String[] args) {
int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
System.out.println("Maximum sum subarray: " + maxSubarraySum(arr));
}
}
24. Find the Sum of the Digits of a Number in an Array.
public class SumOfDigits {
public static int sumOfDigits(int[] arr) {
int sum = 0;
for (int num : arr) {
sum += sumDigits(num);
}
return sum;
}
private static int sumDigits(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
public static void main(String[] args) {
int[] arr = {123, 456, 789};
System.out.println("Sum of digits: " + sumOfDigits(arr));
}
}
25. Reverse an Array In-Place.
public class ReverseArray {
public static void reverse(int[] arr) {
int start = 0, end = arr.length - 1;
while (start < end) {
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("Reversed array: " + Arrays.toString(arr));
}
}
26. Check if an Array is Palindrome.
public class PalindromeArray {
public static boolean isPalindrome(int[] arr) {
int start = 0, end = arr.length - 1;
while (start < end) {
if (arr[start] != arr[end]) {
return false;
}
start++;
end--;
}
return true;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 1};
System.out.println("Is palindrome: " + isPalindrome(arr));
}
}
27. Check if an Array is Sorted.
public class SortedArray {
public static boolean isSorted(int[] arr) {
for (int i = 1; i < arr.length; i++) {
if (arr[i - 1] > arr[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println("Is array sorted? " + isSorted(arr));
}
}
29. Find the Pair with the Smallest Difference in an Array.
import java.util.*;
public class SmallestDifferencePair {
public static int[] findSmallestPair(int[] arr) {
Arrays.sort(arr);
int minDiff = Integer.MAX_VALUE;
int[] pair = new int[2];
for (int i = 1; i < arr.length; i++) {
int diff = arr[i] - arr[i - 1];
if (diff < minDiff) {
minDiff = diff;
pair[0] = arr[i - 1];
pair[1] = arr[i];
}
}
return pair;
}
public static void main(String[] args) {
int[] arr = {1, 5, 9, 13, 17};
System.out.println("Pair with smallest difference: " +
Arrays.toString(findSmallestPair(arr)));
}
}
31. Check if an Array Contains Duplicate Elements.
import java.util.*;
public class ContainsDuplicate {
public static boolean containsDuplicate(int[] arr) {
Set<Integer> set = new HashSet<>();
for (int num : arr) {
if (set.contains(num)) {
return true;
}
set.add(num);
}
return false;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 2};
System.out.println("Contains duplicate: " + containsDuplicate(arr));
}
}