What’s wrong with my code ?
Issue with code
n=int(input())
li=[int(ele) for ele in input().strip().split()]
left=0
right=n-1
area=0
while left<=right:
area=max(area,min(li[left],li[right])*(right-left))
if li[left]<li[right]:
left+=1
else:
right-=1
print(area)
hello @ritvikagrawal1 your logic is not correct.
dry run for this test case:
6
3 0 0 2 0 4
correct ouput is 10.
the logic is you have to create 2 list storing the maximum from left to right and the maximum frim right to left.
like in the above example.
maximum from left to right is:3 3 3 3 3 4
maximum from right to left is:4 4 4 4 4 4
you haveto min(from both the arrays and stort at the corresponding index)
like:
3 3 3 3 3 4
and subtract the corresponding element from the oroginal list which youhave in input.
so the final array will be
0 3 3 1 3 0
and then you have to take the sum of all these estimations.
that will be 10.
try to frame your logic in this way.
Happy Learning !!
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.