You are viewing a single comment's thread. Return to all comments →
Test case 3 and 4 are timed out for me. My code is in python as bellow. Do some one know what is wrong here?
for i in range(1,len(a)):
if sum(a[:i]) == sum(a[i+1:]): return "YES"
else : return "NO"
In python, sum(n) function has O(n) time. Because each loop time, you calculate again "left list" and "rigth list", so I can say that your program has O(n^2) time.
To solve this, you should calculate sum_left and sum_right at 0 position outsite your loop, then each loop time, you to do:
sum_left += arr[i-1]
sum_right -= arr[i]
Then compare sum_left and sum_right. With solution, your program will only take O(n) time