ITGUY01 - Editorial

Problem: CodeChef: Practical coding for everyone

Difficulty:
Moderate

Solution:
#include <bits/stdc++.h>
using namespace std;

// driver code
int main()
{

ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--){
    int n,k;
    cin>>k>>n;
    int arr[n];
    priority_queue <int , vector<int> , greater<int>> pq;
    for(int i=0;i<n;i++){
    	cin>>arr[i];
    	cout<<arr[i]<<" ";
    }
    cout<<"\n";
    for(int i=0;i<n;i++){
        if(pq.size()<k)
        { 
            pq.push(arr[i]);
            if(pq.size()==k) cout<<pq.top()<<" ";
            else cout<<"0 ";
        }
        else if(pq.size()==k && arr[i]>pq.top())
        {   
            pq.pop();
            pq.push(arr[i]);
            cout<<pq.top()<<" ";
        }
        else if(pq.size()==k && arr[i]<=pq.top())cout<<pq.top()<<" ";
    }
    cout<<"\n";
}
//code
return 0;

}

Time: 0.53sec

Thanks and Regards

Dude plz,also provide editorial for “couple goals problem”.

Seriously??!! You asked to print 0 for numbers ,if X’th number for the given number is not present, and in the editorial you have printed -1!!

1 Like