-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkthlargest.cpp
More file actions
107 lines (98 loc) · 2.1 KB
/
kthlargest.cpp
File metadata and controls
107 lines (98 loc) · 2.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
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define MOD 1000000007
void swap(int *x, int *y);
class MaxHeap
{
int *harr;
int capacity;
int heap_size;
public:
MaxHeap(int a[], int size); // Constructor
void maxHeapify(int i); //To maxHeapify subtree rooted with index i
int parent(int i) { return (i-1)/2; }
int left(int i) { return (2*i + 1); }
int right(int i) { return (2*i + 2); }
int extractMax(); // extracts root (maximum) element
int getMax() { return harr[0]; } // Returns maximum
void replaceMax(int x) { harr[0] = x; maxHeapify(0); }
};
MaxHeap::MaxHeap(int a[], int size)
{
heap_size = size;
harr = a; // store address of array
int i = (heap_size - 1)/2;
while (i >= 0)
{
maxHeapify(i);
i--;
}
}
int MaxHeap::extractMax()
{
if (heap_size == 0)
return INT_MAX;
int root = harr[0];
if (heap_size > 1)
{
harr[0] = harr[heap_size-1];
maxHeapify(0);
}
heap_size--;
return root;
}
void MaxHeap::maxHeapify(int i)
{
int l = left(i);
int r = right(i);
int largest = i;
if (l < heap_size && harr[l] > harr[i])
largest = l;
if (r < heap_size && harr[r] > harr[largest])
largest = r;
if (largest != i)
{
swap(&harr[i], &harr[largest]);
maxHeapify(largest);
}
}
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int kthSmallest(int arr[], int n, int k)
{
MaxHeap mh(arr, k);
for (int i=k; i<n; i++)
if (arr[i] < mh.getMax())
mh.replaceMax(arr[i]);
return mh.getMax();
}
void CowboyBebop(){
int n ;
cin >> n ;
int a[n] ;
for(int i = 0 ; i < n ; i++){
cin >> a[i] ;
}
int k ;
cin >> k ;
cout << kthSmallest(a,n,k) << endl ;
}
using namespace std;
int main(void){
std::ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t = 1;
// cin >> t;
while(t--){
CowboyBebop();
}
return 0;
}