Created
September 8, 2015 13:34
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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