Array Equilibrium Index
Find and return the equilibrium index of an array. Equilibrium index of an array is an index i such that the sum of elements at indices less than i is equal to the sum of elements at indices greater than i.
Element at index i is not included in either part.
If more than one equilibrium index is present, you need to return the first one. And return -1 if no equilibrium index is present.
Input format :
Constraints:
Sample Input :
Sample Output :
3
This file contains hidden or 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 equilibrium(arr): | |
total_sum = sum(arr) | |
leftsum = 0 | |
for i, num in enumerate(arr): | |
total_sum -= num | |
if leftsum == total_sum: | |
return i | |
leftsum += num | |
return -1 | |
n=int(input()) | |
arr = list(map(int,input().split())) | |
print(equilibrium(arr)) |
Comments
Post a Comment
Please give us your valuable feedback