MCPcopy Create free account
hub / github.com/geekcomputers/Python / equilibrium

Function equilibrium

Python-Array-Equilibrium-Index.py:19–34  ·  view source on GitHub ↗
(arr)

Source from the content-addressed store, hash-verified

17
18
19def equilibrium(arr):
20 # finding the sum of whole array
21 total_sum = sum(arr)
22 leftsum = 0
23 for i, num in enumerate(arr):
24 # total_sum is now right sum
25 # for index i
26 total_sum -= num
27
28 if leftsum == total_sum:
29 return i
30 leftsum += num
31
32 # If no equilibrium index found,
33 # then return -1
34 return -1
35
36
37n = int(input())

Calls

no outgoing calls

Tested by

no test coverage detected