# LunchTime Box Game Problem

BoxGame help
What is problem with this solution can any one help?

#include
#include
#include
#define ll long long int
using namespace std;
#include

main()
{
ll t;
cin>>t;

``````while(t--)
{
ll n,k,p;
cin>>n>>k>>p;

ll a[n],b[n];

for(ll i=0;i<n;i++)
{
cin>>a[i];

b[i]=a[i];
}
sort(b,b+n);

ll mi=b[0];
ll mxi=b[n-1];

for(ll i=0;i<n;i++)
{
if(a[i]==mi)
mi=i;

if(a[i]==mxi)
mxi=i;
}
ll starting =0;
if(p==0)
{
starting=mxi;
p=1;
}else
{
p=0;
starting=mi;
``````

}
k–;
k=k%2;

``````while(k--)
{
if(p==0)
{
if(starting==0)
starting=1;
else if(starting ==n-1)
starting=n-2;
else
{
if(a[starting+1]>a[starting-1])
starting=starting+1;
else
starting=starting-1;

}

p=1;
}
else
{

if(starting==0)
starting=1;
else if(starting ==n-1)
starting=n-2;
else
{
if(a[starting+1]<a[starting-1])
starting=starting+1;
else
starting=starting-1;

}

p=0;
}

}

cout<<a[starting]<<endl;
``````

}
}

you can’t sort the array. you need to maintain the given order. it’s given in question he can move right or left.you can’t change right or left element.

approach is.
for odd k. if p 0 then maximum number of the entire array. for p 1 minimum number of the entire array.
for even k you need to find max(min(a[i-1],a[i+1])) when p is 0 and min(max(a[i-1],a[i+1])) for every valid i.hope you can figure out why are we applying this approach.