-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKthLargestElementInArray.java
More file actions
475 lines (367 loc) · 14.1 KB
/
KthLargestElementInArray.java
File metadata and controls
475 lines (367 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package Algorithms.HeapAlgos;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Random;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 05 March 2025
* @link 215. Kth Largest Element in an Array <a href="https://leetcode.com/problems/kth-largest-element-in-an-array/">LeetCode link</a>
* @topics Array, Divide and Conquer, Sorting, Heap (PriorityQueue), QuickSelect, BucketSort
* @companies Google(10), Meta(8), Amazon(6), Apple(4), Microsoft(3), Instacart(2), LinkedIn(7), Bloomberg(4), Oracle(3), TikTok(7), Applied Intuition(7), Uber(5), TCS(4), Tinkoff(4), Goldman Sachs(3), Citadel(3), Splunk(3), NetApp(3), Infosys(2)
Here the QuickSelect or QuickSort is "Divide and Conquer" Algorithm
*/
public class KthLargestElementInArray {
public static void main(String[] args) {
int k = 2;
int[] nums = new int[]{3,2,1,5,6,4};
System.out.println("findKthLargestUsingInBuiltSortMethod => " + findKthLargestUsingInBuiltSortMethod(nums, k));
nums = new int[]{3,2,1,5,6,4};
System.out.println("findKthLargest Using PriorityQueue with n size => " + findKthLargestUsingPqWithNSize(nums, k));
nums = new int[]{3,2,1,5,6,4};
System.out.println("findKthLargest Using PriorityQueue with k size 🔥 => " + findKthLargestUsingPqWithKSize(nums, k));
nums = new int[]{3,2,1,5,6,4};
System.out.println("findKthLargest Using Min Max Range BucketSort => " + findKthLargestUsingMinMaxRangeBucketSort(nums, k));
nums = new int[]{3,2,1,5,6,4};
System.out.println("findKthLargest Using QuickSort => " + findKthLargestUsingQuickSort(nums, k));
nums = new int[]{3,2,1,5,6,4};
System.out.println("findKthLargest Using QuickSort2 => " + findKthLargestUsingQuickSort2(nums, k));
nums = new int[]{3,2,1,5,6,4};
System.out.println("findKthLargest Using MinHeap Heapify Down => " + findKthLargestUsingMinHeapHeapifyDown1(nums, k));
nums = new int[]{3,2,1,5,6,4};
System.out.println("findKthLargest Using MaxHeap Heapify Down => " + findKthLargestUsingMaxHeapHeapifyDown(nums, k));
}
/**
* @TimeComplexity O(nlogn)
* @SpaceComplexity O(1)
*/
public static int findKthLargestUsingInBuiltSortMethod(int[] nums, int k) {
int kthLarge = Integer.MIN_VALUE;
Arrays.sort(nums);
kthLarge = nums[nums.length - k];
kthLarge = Arrays.stream(nums).sorted().toArray()[nums.length - k]; // Arrays.sort(nums); nums[nums.length - k];
// or
kthLarge = Arrays.stream(nums).sorted().skip(nums.length - k).findFirst().getAsInt();
// or
kthLarge = Arrays.stream(nums).boxed().sorted(Comparator.reverseOrder()).skip(k - 1).findFirst().get();
// or
kthLarge = Arrays.stream(nums).boxed().sorted(Comparator.reverseOrder()).limit(k).findFirst().get();
// or
kthLarge = Arrays.stream(nums).boxed().sorted(Comparator.reverseOrder()).mapToInt(i -> i).toArray()[k - 1];
return kthLarge;
}
/**
* @TimeComplexity O(nlogn)
* @SpaceComplexity O(n)
*/
public static int findKthLargestUsingPqWithNSize(int[] nums, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
for(int x: nums) pq.offer(x);
int res = 0;
while(k-- > 0) res = pq.poll();
return res;
}
public static int findKthLargestUsingPqWithNSize2(int[] nums, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
for(int x: nums) pq.offer(x);
while(--k > 0) pq.poll();
return pq.poll();
}
/**
* @TimeComplexity O(nlogk)
* @SpaceComplexity O(k)
*
* Here, the pq.offer() methods sum is not nlogn, it's nlogk
* Cause if your heap only holds O(k) Elements i.e size==k, then heap operations only cost u O(log k) TimeComplexity, not O(log n)
* --> for n elements it's nlogk
*/
public static int findKthLargestUsingPqWithKSize(int[] nums, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int num : nums) {
pq.offer(num);
if (pq.size() > k) { // maintain k size
pq.poll(); // removes the smallest element
}
}
return pq.peek(); // or pq.poll(); pq contains only k largest elements & the head will be the kth largest of the array
}
/**
* @TimeComplexity O(n+r) -> n = nums.length, r = range of nums == max-min+1 == Bucket size
* @SpaceComplexity O(r)
*
* How's the Bucket Sort work with negative numbers?
* cause "num - minValue" will never be negative, it will always be >= 0
* So, we can use it as an index in the count array.
*/
public static int findKthLargestUsingMinMaxRangeBucketSort(int[] nums, int k) {
int minValue = Arrays.stream(nums).min().getAsInt();
int maxValue = Arrays.stream(nums).max().getAsInt();
int[] count = new int[maxValue - minValue + 1]; // bucket size
// bucket sort with dupes
for (int num : nums) {
count[num - minValue]++;
}
int remaining = k;
for (int i = count.length - 1; i >= 0; i--) {
remaining -= count[i];
if (remaining <= 0) {
return i + minValue;
}
}
return -1; // This line should not be reached
}
/**
* @TimeComplexity O(n) in average and O(n^2) in worst, but slower than #findKthLargestUsingQuickSort2()
* @SpaceComplexity O(n)
*/
public static int findKthLargestUsingQuickSort(int[] nums, int k) {
List<Integer> list = new ArrayList<>();
for (int num: nums) {
list.add(num);
}
return quickSelect(list, k);
}
private static int quickSelect(List<Integer> nums, int k) {
int pivotIndex = new Random().nextInt(nums.size());
int pivot = nums.get(pivotIndex);
List<Integer> less = new ArrayList<>(); // less than pivot -- LEFT side
List<Integer> equal = new ArrayList<>(); // equal to pivot -- MIDDLE side
List<Integer> greater = new ArrayList<>(); // greater than pivot -- RIGHT side
// 1. Partitioning the given list into three parts -----
for (int num: nums) {
if (num > pivot) {
greater.add(num);
} else if (num < pivot) {
less.add(num);
} else {
equal.add(num);
}
}
// 2. Choose the partition to search in -----
// isInside greater?
if (k <= greater.size()) {
return quickSelect(greater, k);
}
// isInside less?
else if (greater.size() + equal.size() < k) {
return quickSelect(less, k - greater.size() - equal.size());
}
// so definitely inside equal
else
return pivot; // same as (greater.size() < k) && (greater.size() + equal.size() >= k)
// or just use if statements like below
// if (k <= greater.size()) {
// return quickSelect(greater, k);
// }
// if (greater.size() + equal.size() < k) {
// return quickSelect(less, k - greater.size() - equal.size());
// }
// return pivot; // or return (greater.size() + equal.size() >= k)? equal.get(0) : -1;
}
/**
* TLE for very large arrays
* @TimeComplexity O(n) in average and O(n^2) in worst
* @SpaceComplexity O(1)
*/
public static int findKthLargestUsingQuickSort2(int[] nums, int k) {
k = nums.length - k; // new k in ascendingOrder & it's index
return quickSelect(nums, 0, nums.length - 1, k);
}
private static int quickSelect(int[] nums, int l, int r, int k) {
// if (l == r) {
// return nums[l];
// }
int pivot = nums[r]; // always select first element as pivot
int p = l;
for (int i = l; i < r; i++) {
if (nums[i] <= pivot) {
int temp = nums[p];
nums[p] = nums[i];
nums[i] = temp;
p++;
}
}
int temp = nums[p];
nums[p] = nums[r];
nums[r] = temp;
if (p > k) {
return quickSelect(nums, l, p-1, k);
} else if (p < k) {
return quickSelect(nums, p+1, r, k);
} else {
return nums[p];
}
}
/**
* @TimeComplexity O(n) in average and O(n^2) in worst
* @SpaceComplexity O(1)
*/
public static int findKthLargestUsingQuickSort3(int[] nums, int k) {
int targetIdx = nums.length - k; // targetIndex in asc sorted nums arr or new k in ascendingOrder i.e it's index after sort
return quickSelect3(nums, 0, nums.length - 1, targetIdx);
}
// quick sort
private static int quickSelect3(int[] nums, int left, int right, int targetIdx) {
if (left == right) {
return nums[left];
}
int pivot = nums[new Random().nextInt(right-left+1)+left]; // or nums[right] or nums[left] - it can be anything in the window
int low = left;
int high = right;
// now move sort nums as per pivot value i.e leftSide < pivot < rightSide
while (low <= high) {
while (low <= high && nums[low] < pivot) {
low++;
}
while (low <= high && nums[high] > pivot) {
high--;
}
if (low <= high) {
// swap
int temp = nums[low];
nums[low] = nums[high];
nums[high] = temp;
// decrease window
low++;
high--;
}
}
if (targetIdx <= high) {
return quickSelect3(nums, left, high, targetIdx);
} else if (targetIdx >= low) {
return quickSelect3(nums, low, right, targetIdx);
} else {
return nums[targetIdx];
}
}
/**
* @TimeComplexity O(n) in average and O(n^2) in worst
* @SpaceComplexity O(K)
*
* Down-Heapify (Percolate Down) ---> minHeap
*/
public static int findKthLargestUsingMinHeapHeapifyDown1(int[] nums, int k) {
int[] minHeap = Arrays.copyOf(nums, k);
buildMinHeap(minHeap);
for (int i = k; i < nums.length; i++) {
if (nums[i] > minHeap[0]) {
minHeap[0] = nums[i];
minHeapHeapifyDown(minHeap, 0);
}
}
return minHeap[0];
}
public static int findKthLargestUsingMinHeapHeapifyDown2(int[] nums, int k) {
buildMinHeap(nums);
for (int i = nums.length - 1; i >= k - 1; i--) {
int temp = nums[0];
nums[0] = nums[i];
nums[i] = temp;
minHeapHeapifyDown(nums, 0, i);
}
return nums[k - 1];
}
public static void buildMinHeap(int[] heap) {
for (int i = heap.length / 2 - 1; i >= 0; i--) {
minHeapHeapifyDown(heap, i);
}
}
public static void minHeapHeapifyDown(int[] heap, int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < heap.length && heap[left] < heap[smallest]) {
smallest = left;
}
if (right < heap.length && heap[right] < heap[smallest]) {
smallest = right;
}
if (smallest != i) {
int temp = heap[i];
heap[i] = heap[smallest];
heap[smallest] = temp;
minHeapHeapifyDown(heap, smallest);
}
}
public static void minHeapHeapifyDown(int[] heap, int i, int n) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && heap[left] < heap[smallest]) {
smallest = left;
}
if (right < n && heap[right] < heap[smallest]) {
smallest = right;
}
if (smallest != i) {
int temp = heap[i];
heap[i] = heap[smallest];
heap[smallest] = temp;
minHeapHeapifyDown(heap, smallest);
}
}
/**
* @TimeComplexity O(n) in average and O(n^2) in worst
* @SpaceComplexity O(1)
*
* Up-Heapify (Percolate Up) ---> maxHeap
*/
public static int findKthLargestUsingMaxHeapHeapifyDown(int[] nums, int k) {
buildMaxHeap(nums);
for (int i = nums.length - 1; i >= nums.length - k; i--) {
int temp = nums[0];
nums[0] = nums[i];
nums[i] = temp;
maxHeapHeapifyDown(nums, 0, i);
}
return nums[nums.length - k];
}
public static void buildMaxHeap(int[] heap) {
for (int i = heap.length / 2 - 1; i >= 0; i--) {
maxHeapHeapifyDown(heap, i, heap.length); // or maxHeapHeapifyDown(heap, i);
}
}
private static void maxHeapHeapifyDown(int[] nums, int i, int n) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && nums[left] > nums[largest]) {
largest = left;
}
if (right < n && nums[right] > nums[largest]) {
largest = right;
}
if (largest != i) {
int temp = nums[i];
nums[i] = nums[largest];
nums[largest] = temp;
maxHeapHeapifyDown(nums, largest, n);
}
}
public static void heapifyUp(int[] heap, int i) {
while (i > 0) {
int parent = (i - 1) / 2;
if (heap[parent] > heap[i]) { // min heap condition
int temp = heap[parent];
heap[parent] = heap[i];
heap[i] = temp;
i = parent;
} else {
break;
}
}
}
public static int findKthLargest2(int[] nums, int k) {
int[] n = new int[20001];
for(int num:nums) {
n[num+10000]++;
}
for(int i=n.length-1; i>=0; i--) {
k = k-n[i];
if(k<=0) return i-10000;
}
return -1;
}
}