-
Notifications
You must be signed in to change notification settings - Fork 0
/
KLargestElements
70 lines (54 loc) · 1.36 KB
/
KLargestElements
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
You are given with an integer k and an array of integers that contain numbers in random order.
Write a program to find k largest numbers from given array. You need to save them in an array and return it.
Time complexity should be O(nlogk) and space complexity should be not more than O(k).
Order of elements in the output is not important.
Input Format :
Line 1 : Size of array (n)
Line 2 : Array elements (separated by space)
Line 3 : Integer k
Output Format :
k largest elements
Sample Input :
13
2 12 9 16 10 5 3 20 25 11 1 8 6
4
Sample Output :
12
16
20
25
#include <iostream>
using namespace std;
#include "Solution.h"
int main() {
int size;
cin >> size;
int *input = new int[1 + size];
for(int i = 0; i < size; i++)
cin >> input[i];
int k;
cin >> k;
vector<int> output = kLargest(input, size, k);
for(int i = 0; i < output.size(); i++)
cout << output[i] << endl;
return 0;
}
#include <queue>
vector<int> kLargest(int input[], int n, int k){
priority_queue<int, vector <int>, greater<int>> pq;
for(int i = 0; i < k; i++) {
pq.push(input[i]);
}
for(int i = k; i < n; i++) {
if(input[i]>pq.top()){
pq.push(input[i]);
pq.pop();
}
}
vector<int> ans;
while(!pq.empty()){
ans.push_back(pq.top());
pq.pop();
}
return ans;
}