Skip to content

Commit a5ed487

Browse files
authored
Min-Heap
1 parent 5d6f782 commit a5ed487

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

K Sorted Array/program.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
vector <int> nearlySorted(int arr[], int num, int K)
2+
{
3+
// Your code here
4+
vector<int>ans;
5+
priority_queue <int, vector<int>, greater<int>> pq;
6+
for(int i=0;i<num;i++)
7+
{
8+
pq.push(arr[i]);
9+
if(pq.size()>K)
10+
{
11+
ans.push_back(pq.top());
12+
pq.pop();
13+
}
14+
}
15+
while(pq.size()!=0)
16+
{
17+
ans.push_back(pq.top());
18+
pq.pop();
19+
}
20+
21+
return ans;
22+
}

0 commit comments

Comments
 (0)