Well this solution can be optimized more in terms of space requirement by not taking the array. Instead take an element one by one and increment the count. If the input turns out to be zero then set count equal to zero
The code can be
your solution is wrong for some inputs where N = 1… since j is always starting at 1…
it will not enter the for loop and the maxfin will remain equals to 0… the answer for all N = 1 is 1 if a[0] != 0…
n = int(input())
li = list(map(int,input().split()))
count = 0
maximum = 0
i = 0
while i < len(li):
if li[i]>0:
count = count+1
if maximum <= count:
maximum = count
else:
count = 0
i+=1
print(maximum)
For the following I get WA:
n = int(input())
li = list(map(int,input().split()))
count = 0
maximum = 0
i = 0
while i < len(li):
if li[i]>0:
count = count+1
else:
if maximum <= count:
maximum = count
count = 0
i+=1
print(maximum)
Any Idea on what test case might the one with WA fail? Seems like the same code to me.
Here is a link to my solution in Python 3.6. I follow a different way to get to the solution but one of the test case seems to be failing. Can anyone help me find out what it might be
hey @shivamgutgutia,
Congrats on posting your first query!
Here is a test case in which give a wrong answer
3
10 10 10
the correct answer is 3 but your code gives 1.
you are taking nums input as
nums=’ '.join(input().strip().split())
Remember that input() will cast the input as a string. Thus for above mention input, your nums will be “10 10 10”.
If you split this string with ‘0’ nums become [“1”, " 1", " 1"] .
Thanks.