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.
- Calculate the total sum of every number in the array.
- Iterate through the array using a loop, keeping a running
leftSum. - In each step, calculate the
rightSumusing the logic:TotalSum - leftSum - currentElement. - If the
leftSummatches therightSum, print the current index and exit. - If you finish the loop without finding a match, print -1.
01EXAMPLE 1
Input
[1, 3, 5, 2, 2]Output
2Explanation: Index 2: (1+3) = 4, (2+2) = 4.
Constraints
- O(n) time complexity.
ArraysLogic
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
nums[1,3,5,2,2]
Expected Output
2
Click RUN to test your solution