-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathKthSmallestElement.cpp
66 lines (57 loc) · 1.34 KB
/
KthSmallestElement.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
/*
Following is the code to find the Kth smallest element in an array
Approach:
1. Maintain a max heap of size k.
2. Traverse the array and remove the top element if it becomes
greater than the current element of the array.
3. The top element of the heap after the traversal of the array
is the Kth smallest element of the array.
*/
#include<bits/stdc++.h>
using namespace std;
int findkthsmallest(int arr[],int n,int k)
{
//creating a max heap pf size k
priority_queue<int>pq;
//iterating through the elements and storing them in the max heap
for(int i=0;i<n;i++)
{
//maintaing size of heap as k
if(i<k)pq.push(arr[i]);
else
{
if(pq.top()>arr[i])
{
pq.pop();
pq.push(arr[i]);
}
}
}
return pq.top();
}
int main()
{
cout<<"Enter the size of the array: ";
int n,k;
cin>>n;
cout<<"Enter k: ";
cin>>k;
int arr[n];
cout<<"Enter array elements: ";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"The Kth Smallest Element: "<<findkthsmallest(arr,n,k)<<endl;
return 0;
}
/*
Sample Input:
Enter the size of the array: 8
Enter k: 4
Enter array elements: 7 8 9 5 7 2 4 10
Sample Output:
The Kth Smallest Element: 7
Time Complexity: O(nlogk)
Space Complexity: O(k)
*/