Equilibrium Point

HardAcc. 72.8%
+45 XP 25

The Balanced Pivot (Equilibrium)

Imagine you are a tightrope walker trying to find the perfect point of balance. In an array, the "Equilibrium Point" is an index where the sum of all elements to its left is exactly equal to the sum of all elements to its right. Finding this point allows us to "split" a dataset into two equally weighted halves.

The Assignment

Your mission is to find the index of balance. Your function receives an array nums.

  1. Calculate the total sum of every number in the array.
  2. Iterate through the array using a loop, keeping a running leftSum.
  3. In each step, calculate the rightSum using the logic: TotalSum - leftSum - currentElement.
  4. If the leftSum matches the rightSum, print the current index and exit.
  5. If you finish the loop without finding a match, print -1.

01EXAMPLE 1

Input[1, 3, 5, 2, 2]
Output2

Explanation: Index 2: (1+3) = 4, (2+2) = 4.

Constraints

  • O(n) time complexity.
ArraysLogic
JavaScript
Loading...
1 Hidden

Input Arguments

nums[1,3,5,2,2]

Expected Output

2

Click RUN to test your solution