283. Finding Equilibrium Index in an Array
Given a 0-indexed integer array nums, find the leftmost equilibrium index.
An equilibrium index is an index at which the sum of elements on its left is equal to the sum of elements on its right. That is, nums[0] + nums[1] + ... + nums[equilibriumIndex-1] == nums[equilibriumIndex+1] + nums[equilibriumIndex+2] + ... + nums[nums.length-1].
- If equilibriumIndex == 0, the left side sum is considered to be 0.
- Similarly, if equilibriumIndex == nums.length - 1, the right side sum is considered to be 0.
Return the leftmost equilibrium index that satisfies the condition, or -1 if there is no such index.
Example 1:
Input: nums = [2, 3, -1, 8, 4]
Output: 3
Explanation:
The sum of the numbers before index 3 is: 2 + 3 + -1 = 4
The sum of the numbers after index 3 is: 4 = 4
Example 2:
Input: nums = [1, -1, 4]
Output: 2
Explanation:
The sum of the numbers before index 2 is: 1 + -1 = 0
The sum of the numbers after index 2 is: 0
Now Your Turn!
Pick the correct output for the given inputInput: nums = [4, -5, 3, 1, 1]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= nums.length <= 105
- -105 <= nums[i] <= 105