-
Notifications
You must be signed in to change notification settings - Fork 0
/
kthlarge.cpp
83 lines (67 loc) · 1.4 KB
/
kthlarge.cpp
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
@chiranjeevi
Date: 18-May-2017
#include <iostream>
#include <random>
#include <algorithm>
using namespace std;
void print(int arr[], int n){
for(int i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
void exchange(int arr[], int i, int j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
int partition(int arr[], int lo, int hi){
int i=lo,j=hi+1;
int pivot=lo;
if(lo==hi)
return lo;
while(true){
while(arr[++i]<arr[pivot]){
if(i>=hi)
break;
}
while(arr[--j]>arr[pivot]){
if(j<=lo)
break;
}
if(i>=j)
break;
exchange(arr,i,j);
}
exchange(arr,pivot,j);
return j;
}
int findKthLargest(int arr[], int lo, int hi, int k){
if(k>hi || k<lo)
return -1;
while(true){
int j=partition(arr,lo,hi);
if(j==k)
return arr[j];
else if(k<j){
hi=j-1;
}
else{
lo=j+1;
}
}
}
int main() {
int n,range,k,element;
cin>>n>>range>>k;
int arr[n];
default_random_engine gen;
uniform_int_distribution<int> dist(1,range);
for(int i=0; i<n; i++)
arr[i]=19-i;
random_shuffle(arr,arr+n); //if input taken from user
print (arr,n);
element=findKthLargest(arr,0,n-1,k);
cout<<element<<endl;
print(arr,n);
return 0;
}