**PROBLEM SETTER** -

ZCO 2014

**DIFFICULTY** -

Beginner

**PREREQUISITES** -

Implementation

**SOLUTION IN SHORT** -

There are no techniques or optimizations. Do as directed!!!

**EXPLANATION** -

There is a while loop which has a condition of breaking to be when input numer is 0. You can maintain a variable i denoting the current position of crane. You also have stored the stack sizes in an array a. For drop command, check if a[i]+1 is greater than h, if not, increment a[i]. For pick command, check if a[i] is positive, if yes, decrement a[i]. For moving left, do i-- and for moving right, do i++ and accordingly check if i > 1 (for moving left) and i < N (for moving right). Outside the while loop, after it has ended, output the array a.

**TIME COMPLEXITY** -

O(number of queries/operations)

#include

using namespace std;

int main()

{

int n=0,i=0,h=0,a[100000],ch,j=0;

cin>>n;

cin>>h;

for(i=0;i<n;i++)

cin>>a[i];

i=0;

while(ch !=0)

{

cin>>ch;

switch(ch)

{

case 1:

{

if(i>0 && i<=n )

{

i–;ch=-1;

}

}

break;

case 2:

{

if(i>=0&&i<n)

{

i++;ch=-1;

}

}

break;

case 3:

{

if(a[i]>0)

{

a[i]=a[i]-1;ch=-1;

}

}

break;

case 4:

{

if(a[i]<h)

{

a[i]=a[i]+1;ch=-1;

}

}

break;

case 0:

break;

default:

{

ch=-1;

}

break;

}

}

for(i=0;i<n;i++)

{

cout<<a[i];

}

return 0;

}

*In Python 3.6:*

```
l, lim = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
commands = list(input())
s, crane = 1, False
for c in commands:
if c == "1" and s != 1:
s -= 1
elif c == "2" and s != l:
s += 1
elif c == "3" and not crane and a[s-1] != 0:
a[s-1] -= 1
crane = True
elif c == "4" and a[s-1] < lim and crane:
a[s-1] += 1
crane = False
elif c == "0":
break
o = ""
for i in a:
o += " " + str(i)
print(o[1:])
```

C++:

```
#include <bits/stdc++.h>
using namespace std;
int main() {
int widthBox, heightBox;
scanf("%d %d", &widthBox, &heightBox);
int box[widthBox];
for (int i = 0; i < widthBox; i++)
cin >> box[i];
int hookLocation = 0, holding = 0;
int input = 0;
while (cin >> input) {
switch (input) {
case 1:
if (hookLocation > 0)
hookLocation--;
break;
case 2:
if (hookLocation < widthBox - 1)
hookLocation++;
break;
case 3:
if (holding == 0 && box[hookLocation] > 0) {
holding++;
box[hookLocation]--;
}
break;
case 4:
if (holding == 1 && box[hookLocation] < heightBox) {
holding--;
box[hookLocation]++;
}
break;
case 0:
break;
}
}
for (int i = 0; i < widthBox; i++)
printf("%d ", box[i]);
return 0;
}
```

@vivek1_007 what happens when the stack is full? does the lifted box simply vanish?

if so, how is the 1st input example justified:

7 4

3 1 2 1 4 0 1

3 2 2 2 2 4 1 3 1 4 0

the output should have been : 2 1 3 0 4 0 1

instead of : 2 1 3 1 4 0 1

anyone kindly help

IN C++

```
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
// number of stacks (n) and max height for stacks (H)
ll n,h,p=0;
cin>>n>>h;
// array for height of stack
ll a[n];
// array for operation
ll b[100005];
// taking height as input
for(ll i=0;i<n;i++){
cin>>a[i];
}
// taking operations as input
ll jim;
while(cin>>jim){
b[p]=jim;
p++;
if(jim==0){
break;
}
}
// variable to know if box is already present at crane or not
// if box is present then k=1 otherwise 0
// initially there was no box at crane
ll k=0;
// variable to determine crane position
// initially crane was at 0th stack
ll j=0;
//traversing through operation array
for(ll i=0;i<p;i++){
// if operation is 0 then we have to quit
if(b[i]==0){
break;
}
// if operation is 1 we have to move left but we cannot go left if we are already at beginning i.e.. 0 .
else if(b[i]==1){
if(j!=0){
j=j-1;
}
}
// if operation is 2 we have to move right but we cannot go right if we are already at end i.e.. n-1 .
else if(b[i]==2){
if(j!=n-1){
j=j+1;
}
}
// if operation is 3 we have to pick the box that means set k=1 and decrease height of current stack by 1.
// But we can't do it if either there is a box already present at crane or there is no box at current stack.
else if(b[i]==3){
if(k!=1 && a[j]!=0){
k=1;
a[j]=a[j]-1;
}
}
// if operation is 4 we have to drop the box that means set k=0 and increase height of current stack by 1.
// But we can't do it if either there is a no box present at crane or height of current stack is equal to H (max height of each stack).
else{
if(k!=0 && a[j]<h){
k=0;
a[j]=a[j]+1;
}
}
}
// print the final heights of stacks
for(ll i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
```

#CAN ANYONE TELL ME THE ERROR IN THIS CODE OF PYTHON 3.6

inputs=input().split(" “)

h=int(inputs[1])

N=input().split()

n=[]

for i in N:

n.append(int(i))

choice=input().split(” ")

pickup=1

left=0

right=len(choice)-1

dropdown=0

position=0

for i in range(0,len(choice)):

if left>len(n):

left=len(n)-1

if int(choice[i])==3: #pickup

if pickup==1:

n[left]-=1

pickup=0

dropdown=1

elif int(choice[i])==4: #drop

if int(n[left])<h:

if dropdown==1:

n[left]+=1

pickup=1

dropdown=0

elif int(choice[i])==1: #left

if left!=0:

left-=1

right+=1

elif int(choice[i])==2: #right

if right!=0:

left+=1

right-=1

elif int(choice[i])==0:

break

for i in n:

print(i,end=’ ')

Rules are:

When the stack is full, and the instruction says drop, you have to ignore the instruction.

When the stack is empty and instruction says pick, ignore the instruction.

If instruction says pick, and crane already has one box, ignore the instruction.

similarly, If instruction says drop, and crane doesn’t have any box, ignore.