HackWithInfy 2021 Sample Question-C (Minimum Withdrawals) Solution

Tricks to solve the problem: First try to make the desired amount from one side of the array. Then check whether amount can be withdrawal from that side and if possible then store the no of withdrawals required.Then start adding from the other side removing one by one elements from the firstly chosen side. Means we will remove elements from the first side and start adding elements from the other side until all the elements of the beginning side is removed in this process continue checking the no of withdrawals and compare with the minimum value of withdrawals. It will be more clear if you observe the code.Before moving to the solution we recommend you to attempt the problem first from yourself then move to the solution part.Here, is our solution to the answer in Python.

```# HackWithInfy Sample Paper Round-1
# Q. Minimum Withdrawals

a=int(input())
b=[]

for i in range(a):
temp=int(input())
b.append(temp)

c=int(input())
if(sum(b)<c):
print(-1)
exit(0)

d=b[::-1]
sum1=0
i=0
j=0
minv=99999

while(i<len(b) and sum1<=c):
sum1=sum1+b[i]
i=i+1
i=i-1
r=i+1
flag=0

if(sum1==c and r<minv):
flag=1
minv=r

while(1):
if(j<len(b) and sum1+d[j]<=c):
sum1=sum1+d[j]
j=j+1
r=r+1
elif(i>=0):
sum1=sum1-b[i]
i=i-1
r=r-1
else:
break
if(sum1==c and r<minv):
minv=r

if(sum1==c and r<minv):
minv=r
if(minv==99999):
print(-1)
else:
print(minv)
```

You Missed

Also Checkout