0% found this document useful (0 votes)
5 views4 pages

Most_Asked_Array_Solutions

Urhr jrjrb krbrbr

Uploaded by

deepshiva1012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Most_Asked_Array_Solutions

Urhr jrjrb krbrbr

Uploaded by

deepshiva1012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Solutions to Most Asked Array Questions

1. Implement a Custom Comparator to Sort an Array Based on Complex


Conditions
Java Code:

import java.util.Arrays;
import java.util.Comparator;

public class CustomSort {


public static void main(String[] args) {
Integer[] arr = {3, 1, 2, 4, 7, 6, 9, 8, 5};

Arrays.sort(arr, new Comparator<Integer>() {


@Override
public int compare(Integer a, Integer b) {
if (a % 2 == 0 && b % 2 == 0) {
return b - a;
} else if (a % 2 != 0 && b % 2 != 0) {
return a - b;
} else {
return (a % 2 == 0) ? -1 : 1;
}
}
});

System.out.println(Arrays.toString(arr));
}
}

2. Find the Kth Smallest/Largest Element in an Unsorted Array


Java Code:

import java.util.PriorityQueue;

public class KthElement {


public static int findKthSmallest(int[] nums, int k) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
for (int num : nums) {
maxHeap.offer(num);
if (maxHeap.size() > k) {
maxHeap.poll();
}
}
return maxHeap.peek();
}

public static int findKthLargest(int[] nums, int k) {


PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for (int num : nums) {
minHeap.offer(num);
if (minHeap.size() > k) {
minHeap.poll();
}
}
return minHeap.peek();
}

public static void main(String[] args) {


int[] arr = {3, 2, 1, 5, 6, 4};
System.out.println(findKthSmallest(arr, 2)); // Output: 2
System.out.println(findKthLargest(arr, 2)); // Output: 5
}
}

3. Search in a Sorted and Rotated Array (With Duplicates)


Java Code:

public class SearchRotatedArray {


public static boolean search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) return true;
if (nums[left] == nums[mid] && nums[mid] == nums[right]) {
left++;
right--;
} else if (nums[left] <= nums[mid]) {
if (nums[left] <= target && target < nums[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return false;
}

public static void main(String[] args) {


int[] arr = {2, 5, 6, 0, 0, 1, 2};
System.out.println(search(arr, 0)); // Output: true
System.out.println(search(arr, 3)); // Output: false
}
}

4. Sort an Almost-Sorted Array (Elements at Most K Away from Correct


Position)
Java Code:

import java.util.PriorityQueue;

public class SortAlmostSorted {


public static int[] sortNearlySorted(int[] arr, int k) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
int[] result = new int[arr.length];
int index = 0;

for (int i = 0; i < arr.length; i++) {


minHeap.offer(arr[i]);
if (minHeap.size() > k) {
result[index++] = minHeap.poll();
}
}
while (!minHeap.isEmpty()) {
result[index++] = minHeap.poll();
}

return result;
}

public static void main(String[] args) {


int[] arr = {6, 5, 3, 2, 8, 10, 9};
int k = 3;
int[] sortedArr = sortNearlySorted(arr, k);
for (int num : sortedArr) {
System.out.print(num + " ");
}
}
}

You might also like