Java Array Problems for Coding Interviews
1. Two Sum
Problem: Find two indices such that their values add up to a given target.
LeetCode Link: [Link]
Java Code:
import [Link];
public class TwoSum {
public static int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < [Link]; i++) {
int diff = target - nums[i];
if ([Link](diff)) {
return new int[]{[Link](diff), i};
}
[Link](nums[i], i);
}
return new int[0];
}
}
2. Best Time to Buy and Sell Stock
Problem: Maximize profit by choosing a day to buy and sell stock.
LeetCode Link: [Link]
Java Code:
public class BuySellStock {
public static int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE;
int maxProfit = 0;
for (int price : prices) {
if (price < minPrice) minPrice = price;
else maxProfit = [Link](maxProfit, price - minPrice);
}
return maxProfit;
}
}
3. Maximum Subarray (Kadane's Algorithm)
Problem: Find the contiguous subarray with the largest sum.
LeetCode Link: [Link]
Java Code:
Java Array Problems for Coding Interviews
public class Kadane {
public static int maxSubArray(int[] nums) {
int current = nums[0], max = nums[0];
for (int i = 1; i < [Link]; i++) {
current = [Link](nums[i], current + nums[i]);
max = [Link](max, current);
}
return max;
}
}
4. Move Zeroes
Problem: Move all zeroes to the end, maintaining order of other elements.
LeetCode Link: [Link]
Java Code:
public class MoveZeroes {
public static void moveZeroes(int[] nums) {
int i = 0;
for (int j = 0; j < [Link]; j++) {
if (nums[j] != 0) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
}
}
}
}
5. Contains Duplicate
Problem: Check if the array contains any duplicates.
LeetCode Link: [Link]
Java Code:
import [Link];
public class Duplicate {
public static boolean containsDuplicate(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int num : nums) {
if ([Link](num)) return true;
[Link](num);
}
return false;
Java Array Problems for Coding Interviews
}
}
6. Sliding Window Maximum
Problem: Find max in every window of size k.
LeetCode Link: [Link]
Java Code:
import [Link].*;
public class SlidingWindowMax {
public static int[] maxSlidingWindow(int[] nums, int k) {
if ([Link] == 0) return new int[0];
int n = [Link];
int[] result = new int[n - k + 1];
Deque<Integer> dq = new LinkedList<>();
for (int i = 0; i < n; i++) {
while (![Link]() && [Link]() < i - k + 1) [Link]();
while (![Link]() && nums[[Link]()] < nums[i]) [Link]();
[Link](i);
if (i >= k - 1) result[i - k + 1] = nums[[Link]()];
}
return result;
}
}
7. Subarray Sum Equals K
Problem: Count the number of subarrays with sum equals k.
LeetCode Link: [Link]
Java Code:
import [Link];
public class SubarraySumK {
public static int subarraySum(int[] nums, int k) {
int count = 0, sum = 0;
HashMap<Integer, Integer> map = new HashMap<>();
[Link](0, 1);
for (int num : nums) {
sum += num;
if ([Link](sum - k)) count += [Link](sum - k);
[Link](sum, [Link](sum, 0) + 1);
}
return count;
}
Java Array Problems for Coding Interviews