I was solving this problem from hackerrank, but i couldn’t pass the last test case. It always gives TLE even if i use multiset which is a data structure that can contain multiple elements which are sorted.
Here is the link to the problem: Click here
My Solution:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(NULL);
int n,x,k,temp;
cin >> n;
multiset <int> a;
for(int i=0;i<n;i++){
cin >> temp;
a.insert(temp);
}
int q;
cin >> q;
while( q--){
cin >> x >> k;
a.insert(x);
auto it=a.begin();
advance(it,k-1);
cout << *it<< '\n';
}
return 0;
}
Can anyone help me how to solve this question ? Please show the code also