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.
@himadri77 it is not necessary to put a manual condition for one element in the array, i think your logic is wrong rather than the author. Here is my code and its working perfectly...
def sherlock_algo(arr):
l = 0
u = len(arr)
index = (l+u)//2
while l<=u :
sum1 = sum(arr[0:index])
sum2 = sum(arr[index+1:])
if sum1 == sum2:
return index
elif sum1 > sum2:
u = index-1
else:
l = index + 1
index = (l + u) // 2
return -1
t = int(input()) #testcases
for i in range(t):
n = int(input()) # length of list
list1 = list(int(x) for x in input().split())
result = sherlock_algo(list1)
if result == -1:
print("NO")
else:
print("YES")
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Array
You are viewing a single comment's thread. Return to all comments →
@himadri77 it is not necessary to put a manual condition for one element in the array, i think your logic is wrong rather than the author. Here is my code and its working perfectly...
def sherlock_algo(arr): l = 0 u = len(arr) index = (l+u)//2 while l<=u : sum1 = sum(arr[0:index]) sum2 = sum(arr[index+1:])
t = int(input()) #testcases for i in range(t):