"Find equilibrium index of an array" Code Answer's
You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "Find equilibrium index of an array" answered properly. Developers are finding an appropriate answer about Find equilibrium index of an array related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like Find equilibrium index of an array. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on Find equilibrium index of an array.
Find equilibrium index of an array
def equilibriumIndex(A):
# `total` stores the sum of all elements in the list
total = sum(A)
# `right` stores the sum of elements of sublist `A[i+1…n)`
right = 0
# maintain a list of indices
indices = []
# traverse the list from right to left
for i in reversed(range(len(A))):
''' `i` is an equilibrium index if the sum of elements of sublist
`A[0…i-1]` is equal to the sum of elements of the sublist
A[i+1…n), i.e., (A[0] + A[1] + … + A[i-1]) =
(A[i+1] + A[i+2] + … + A[n-1]) '''
# sum of elements of the left sublist `A[0…i-1]` is
# (total - (A[i] + right))
if right == total - (A[i] + right):
indices.append(i)
# new right is `A[i] + (A[i+1] + A[i+2] + … + A[n-1])`
right += A[i]
print("Equilibrium Index found at", indices)
All those coders who are working on the Whatever based application and are stuck on Find equilibrium index of an array can get a collection of related answers to their query. Programmers need to enter their query on Find equilibrium index of an array related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about Find equilibrium index of an array for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of Find equilibrium index of an array while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "Find equilibrium index of an array". Visit this developer's friendly online web community, CodeProZone, and get your queries like Find equilibrium index of an array resolved professionally and stay updated to the latest Whatever updates.