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.

python 3 code, easy to understand :) . It takes linear time

defsolve(a):# Complete this functionsummy=sum(a)l=len(a)lefty=0foriinrange(len(a)):current=a[i]summy-=currentiflefty==summy:return'YES'lefty+=currentreturn'NO'

I had a similar idea, but yours is way better. Helped me figure out why mine was so slow:

defsolve(a):# Complete this functioniflen(a)==1:return"YES"foriinrange(1,int(len(a)+1/2)):left=sum(a[:i])right=sum(a[i+1:])ifleft==right:return"YES"breakelse:return"NO"

Instead of computing the sum each time, just subtract. Way more efficient. Thank you for sharing.

## Sherlock and Array

You are viewing a single comment's thread. Return to all comments →

python 3 code, easy to understand :) . It takes linear time

loved it

great !!! best solution ..

I had a similar idea, but yours is way better. Helped me figure out why mine was so slow:

Instead of computing the sum each time, just subtract. Way more efficient. Thank you for sharing.

Nice work!

My first try was to sum both sides of the pivot and I timed out

Then realised sum the whole array and + / - the pivot

I iterated over the array rather than use indexes