Array Problems Using Java
1. Find Kth Largest Element in an Array
Given an array of integers and a value K, find the Kth largest element in
the array.
Sample Input:
Array: [7, 10, 4, 3, 20, 15]
K=3
Sample Output:
10
2. Find Kth Smallest Element in an Array
Given an array of integers and a value K, find the Kth smallest element in
the array.
Sample Input:
Array: [7, 10, 4, 3, 20, 15]
K=3
Sample Output:
3. Binary Search in an Array
Problem Statement:
Given a sorted array and a target element, find the index of the element
using binary search.
If not found, return -1.
Sample Input:
Array: [2, 4, 6, 8, 10, 12]
Target = 8
Sample Output:
4. Two Sum Problem
Given an array of integers and a target value, return the indices of the two
numbers such that they add up to the target.
Sample Input:
Array: [2, 7, 11, 15]
Target = 9
Sample Output:
[0, 1]
5. Three Sum Problem
Given an array of integers, find all unique triplets whose sum is equal to
zero.
Sample Input:
Array: [-1, 0, 1, 2, -1, -4]
Sample Output:
[[-1, -1, 2], [-1, 0, 1]]
6. Rotate Array (Clockwise)
Problem Statement:
Given an array, rotate it to the right (clockwise) by K steps.
Sample Input:
Array: [1, 2, 3, 4, 5]
K=2
Sample Output:
[4, 5, 1, 2, 3]
7. Find All Prime Numbers in an Array
Given an array of integers, print all the prime numbers present in the array.
Sample Input:
Array: [2, 3, 4, 5, 6, 7, 8, 9, 10]
Sample Output:
[2, 3, 5, 7]
8. Maximum Sum Subarray of Size K (Use Sliding window pattern)
Given an array of integers and an integer K, find the maximum sum of any
contiguous subarray of size K.
Sample Input:
Array: [2, 1, 5, 1, 3, 2]
K=3
Sample Output:
9
9. Container With Most Water (Use two pointer method)
Given an array representing heights, find two lines that together with the x-
axis form a container that holds the maximum water.
Sample Input:
Array: [1,8,6,2,5,4,8,3,7]
Sample Output:
49
10. Dutch National Flag (Sort 0s, 1s, 2s) (Use two pointer method)
Given an array containing only 0, 1, and 2, sort the array in-place.
Sample Input:
Array: [2, 0, 2, 1, 1, 0]
Sample Output:
[0, 0, 1, 1, 2, 2]
11. Floyd’s Triangle
Print Floyd’s triangle using numbers.
Sample Input:
N=4
Sample Output:
1
23
456
7 8 9 10
12. Hollow Square
Print a square with only borders filled with *.
Sample Input:
N=4
Sample Output:
****
* *
* *
****