# ZCOPRAC Video Game Help Need in One Condition

I am trying to solve thisproblem. I have implemented the code but there seems to be some mistake with the logic that I am using. I think the problem is when c==4. Can you please help me figure this out.

``````using namespace std;

int main()
{
int n,h;
cin>>n>>h;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int c;
cin>>c;
int j=0;
while(c!=0)
{
bool isPicked = false;
if(c==1 && j>0)
{
j--;
}
if(c==2 && j<n-1)
{
j++;
}
if(c==3)
{
if(!isPicked && a[j] > 0)
{
isPicked = true;
a[j]--;
}
}
if(c==4)
{
if(isPicked && a[j] < h)
{
isPicked = false;
a[j]++;
}
}
cin>>c;
}

for(int i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
``````

In this problem, you just need to simulate the game process and output the answerâ€¦

You may have a look at my code here

There are a number of mistakes in your codeâ€¦

First, when c == 4 && a[j}, add a[j]â€“

Second, when c == 2 && j != n-1 instead of j != n

Third, you are complicating your codeâ€¦

Simply use if-else statements like

if(c==1){

}else if(c==2){

}else if(c==3){

}else if(c==4){

}else{

}

Your code is hard to understand and even harder to find bugâ€¦

1 Like

@taran_1407

Oopsâ€¦

Check here

https://pastebin.com/zw9Vde6q

can you please explain the c==4 part again?

@taran_1407 it should be a[j++] right?Also, I have updated my code still wrong answer

@vijju123 please broâ€¦ I am stuck at this problem for the last few daysâ€¦

Can you please tell me how are you sure of WA? I cannot see you submitting the code anywhere. Need that to see your updated code. Thanks!

@montycs You just need to realise that what information you need to simulate the whole game is current position and we are holding an item or notâ€¦ (pos and hold variable in my codeâ€¦

If c==1 pos-- if pos > 0 (move left if its not already of leftmost positionâ€¦)

If c==2 pos++ if pos < N-1 (i have used 0-based indexing)

If c==3 pick up box if box[pos] >0 AND !hold

``````      box[pos]-- (reduced no of items in current box by one)

hold =true
``````

Similarly if c==4 drop if box[pos]<H AND hold = true

``````     Box[pos]++ (increased count of items by one)

hold = false``````

Your code is almost correct except a silly mistake-

``````int j=0;
while(c!=0)
{
bool isPicked = false;
``````

Every iteration you are setting isPicked=false. I think that statement should be out of while loop, because lets say you picked up a box and set isPicked=true. Then in the immediate next iteration you are setting it false, and this leads to error when drop operations are to be done.

Aside from that it seems fine.

1 Like

@vijju123 Sorry for not including that, hereâ€™s the link https://ideone.com/iO2txU

Thanks for that!

Thank you very much!!

//