Hussain Set Question.
my code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,q;
cin>>n>>q;
if(n==0)return 0;
vector<long long> arr;
while(n!=0)// filling the array
{
int a;
cin>>a;
arr.push_back(a);
n--;
}
sort(arr.begin(),arr.end());
vector<long long> ans;// stores answers for queries
queue<long long> query;// queue for processing what is being removed.. not the query numbers
if(arr.back() == 0)return 0;
int counter=1;// for first push
ans.push_back(arr.back());
query.push(arr.back()/2);// pushed the half of last of arr if its not zero
arr.pop_back();//removed the last index of arr vector
while(!query.empty())
{
int current=query.front();//to compare it with the next last of arr
if(arr.size()!=0&¤t>=arr.back())//we need to push half of current into queue
{
if(current/2!=0)query.push(current/2);
ans.push_back(current);
query.pop();//remove first element since its half is pushed back
}
else if(arr.size()==0 && !query.empty())
{
if(current/2!=0)query.push(current/2);
ans.push_back(current);
query.pop();//remove first element since its half is pushed back
}
else // when the last element of arr is bigger than front of queue
{
if(arr.back()/2!=0)query.push(arr.back()/2);
ans.push_back(arr.back());
arr.pop_back();
}
counter++;
}
for(int i=0;i<q;i++)
{
int idx;
cin>>idx;
cout<<ans[idx-1]<<endl;
}
return 0;
}
please help…