-
Notifications
You must be signed in to change notification settings - Fork 2
/
sort_k_sorted_array.cpp
62 lines (50 loc) · 1.21 KB
/
sort_k_sorted_array.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
/*
Given an array of n elements, where each element is at most k away from its target position. The task is to print array in sorted form.
Input:
First line consists of T test cases. First line of every test case consists of two integers N and K, denoting number of elements in array and at most k positions away from its target position respectively. Second line of every test case consists of elements of array.
Output:
Single line output to print the sorted array.
Constraints:
1<=T<=100
1<=N<=100
1<=K<=N
Example:
Input:
2
3 3
2 1 3
6 3
2 6 3 12 56 8
Output:
1 2 3
2 3 6 8 12 56
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
priority_queue<int, vector<int>, greater<int> >minh;
for(int i=0;i<n;i++){
minh.push(a[i]);
if(minh.size()>k){
cout<<minh.top()<<" ";
minh.pop();
}
}
while(!minh.empty()){
cout<<minh.top()<<" ";
minh.pop();
}
cout<<endl;
}
return 0;
}