-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKthLargestElementInAStream.cpp
More file actions
99 lines (90 loc) · 2.06 KB
/
Copy pathKthLargestElementInAStream.cpp
File metadata and controls
99 lines (90 loc) · 2.06 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
/* GFG
Given an input stream of n integers, find the kth largest element each time when an new element is added to the stream.
Example 1:
Input:
k = 4, n = 6
arr[] = {1,2,3,4,5,6}
Output: -1 -1 -1 1 2 3
Explanation: k = 4
For 1, the 4th largest element doesn't
exist so we print -1.
For 2, the 4th largest element doesn't
exist so we print -1.
For 3, the 4th largest element doesn't
exist so we print -1.
For 4, the 4th largest element is 1
{1, 2, 3, 4}
For 5, the 4th largest element is 2
{2, 3, 4 ,5}
For 6, the 4th largest element is 3
{3, 4, 5, 6}
Example 2:
Input:
k = 1, n = 2
arr[] = {3,4}
Output: 3 4
Your Task:
You are required to complete the method kthLargest() which takes 3 arguments and prints kth largest element -1.
Constraints:
1 <= n <= 10^6
1 <= k <= n
1 <= arr[i] <= 10^5
Expected Time Complexity: O(n*Log(k))
Expected Auxiliary Space: O(k)
*/
class Solution
{
public:
//Function to print kth largest element for each element in the stream.
void kthLargest(int arr[], int n, int k)
{
// your code here
priority_queue<int> min_heap,max_heap;
for(int i=0;i<k-1;i++)
{
min_heap.push(-arr[i]);
cout<<-1<<" ";
}
int kth;
if(min_heap.empty())
{
kth=arr[k-1];
}
else if(-min_heap.top()<arr[k-1])
{
kth=-min_heap.top();
min_heap.pop();
min_heap.push(-arr[k-1]);
}
else
{
kth=arr[k-1];
}
cout<<kth<<" ";
for(int i=k;i<n;i++)
{
if(arr[i]<=kth)
{
max_heap.push(arr[i]);
}
else
{
max_heap.push(kth);
if(min_heap.empty())
kth=arr[i];
else if(-min_heap.top()<arr[i])
{
kth=-min_heap.top();
min_heap.pop();
min_heap.push(-arr[i]);
}
else
{
kth=arr[i];
}
}
cout<<kth<<" ";
}
return;
}
};