We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def candies(n, arr):
sum = 0
i = 0
pre = 1
while i < n:
j = i+1
l = []
no = 0
if j < n:
if arr[i] >= arr[j]:
no += 1
j += 1
while j < n:
if arr[j] < arr[j-1]:
no = no+1
elif arr[j] == arr[j-1]:
l.append(no)
else:
break
j += 1
if i+1 < n and arr[i] == arr[i+1] :
fi = no
elif i+1 < n:
fi = no+1
else :
fi = 0
sum += max(pre, fi)
sum1 = int((no*(no+1))/2)
for ite in l:
sum1 += no-ite+1
sum = sum+sum1
if i == j-1:
pre = pre+1
else:
pre = 2
i = j
return int(sum)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Candies
You are viewing a single comment's thread. Return to all comments →
its not working for most of the cases