Skip to content

Instantly share code, notes, and snippets.

@xeonqq
Created September 8, 2015 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeonqq/206590361a5c62101642 to your computer and use it in GitHub Desktop.
Save xeonqq/206590361a5c62101642 to your computer and use it in GitHub Desktop.
Find an index in an array such that its prefix sum equals its suffix sum.
def solution(A):
# write your code in Python 2.7
s = []
tmp = 0
n = len(A)
for a in A:
tmp = a + tmp
s.append(tmp)
s.append(0)
for i in xrange(0,n):
if s[i-1] == s[n-1]-s[i]:
return i
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment