# HackerRank

My solution to this problem gives WA for some test cases. Kindly look into it and suggest the changes.

#include
#include
#include
#include
#include
using namespace std;

void fastscan(long long &x)
{
bool neg=false;
register int c;
x =0;
c=getchar();
if(c==‘-’)
{
neg = true;
c=getchar();
}
for(;(c>47 && c<58);c=getchar())
x = (x<<1) + (x<<3) +c -48;
if(neg)
x *=-1;
}

void fast(int &x)
{
bool neg=false;
register int c;
x =0;
c=getchar();
if(c==‘-’)
{
neg = true;
c=getchar();
}
for(;(c>47 && c<58);c=getchar())
x = (x<<1) + (x<<3) +c -48;
if(neg)
x *=-1;
}

int main() {
long long n,q,t;
fastscan(n);
fastscan(q);
vector soldiers;
for(int i=0;i<n;i++){
fastscan(t);
soldiers.push_back(t);
}
vector queries;
queries.push_back(0);
queries.push_back(n);
while(q–)
{
int type;
long long x;
fast(type);
fastscan(x);
if(type==1)
{
vector::iterator it = std::find (soldiers.begin(), soldiers.end(), x);
vector::iterator t = std::find (queries.begin(), queries.end(), (long long)(it - soldiers.begin() + 1));
if( t == queries.end()){
queries.push_back((long long)(it - soldiers.begin() + 1));
sort(queries.begin(),queries.end());
}
}
else if(type == 2)
{
for(vector::iterator track=queries.begin(); track != queries.end(); track++)
{
if(track+1 == queries.end())
{
break;
}
else
{
long long start = *track,end = (track+1);
vector::iterator first = soldiers.begin() + start;
vector::iterator last = soldiers.begin() + end;
if ( std::find(first, last, x) != last )
{
printf(“%lld\n”,
(last-1));
break;
}
}
}
}

``````}
return 0;
``````

}