Find Equilibrium Index
An equilibrium index of an array is an index P such that the sum of elements at indices lower than P is equal to the sum of elements at indices higher than P. If P is 0, then the left sum is 0. If P is N-1 (where N is the array length), then the right sum is 0. Your task is to find the smallest equilibrium index in a given array of integers. If no such index exists, return -1.
[ "[-7, 1, 5, 2, -4, 3, 0]" ]
Explanation. At index 3, the element is 2. The sum of elements to its left (-7 + 1 + 5) is -1. The sum of elements to its right (-4 + 3 + 0) is -1. Since both sums are equal, 3 is an equilibrium index. It is the smallest such index.
[ "[]" ]
Explanation. An empty array has no indices, so no equilibrium index can exist. Returns -1.
[ "[-1]" ]
Explanation. At index 0, the element is -1. The sum of elements to its left is 0. The sum of elements to its right is 0. Since both sums are equal, 0 is an equilibrium index.
[ "[-1, 1, 0]" ]
Explanation. At index 0: left sum=0, right sum=1+0=1. Not equilibrium. At index 1: left sum=-1, right sum=0. Not equilibrium. At index 2: left sum=-1+1=0, right sum=0. Equilibrium! So 2 is the smallest.
[ "[-1, -1, -1, -1, 0, -1, -1, -1]" ]
Explanation. At index 4 (value 0): left sum = -1 + -1 + -1 + -1 = -4. Right sum = -1 + -1 + -1 = -3. Not an equilibrium. Wait, I made a mistake in the example, let me correct the output for this specific input to reflect 'no equilibrium index'. Correcting the explanation now. For this specific input, there is no equilibrium index. Let's re-evaluate: Total sum is -8. For an equilibrium index `i`, `leftSum = totalSum - nums[i] - leftSum`. This means `2 * leftSum = totalSum - nums[i]`. So `(totalSum - nums[i])` must be even. Let's check for P=4: `totalSum = -8`, `nums[4] = 0`. `( -8 - 0 ) / 2 = -4`. `leftSum` for P=4 is `(-1) + (-1) + (-1) + (-1) = -4`. So, 4 IS an equilibrium index. My earlier mental math was wrong. The output should be 4.
[ "[]" ]
Explanation. An empty array has no indices, thus no equilibrium index.
[ "[-10, 0, 10]" ]
Explanation. At index 1 (value 0): left sum = -10. Right sum = 10. Not equilibrium. Let's re-check. `[-10, 0, 10]`. Total sum = 0. If P=0, left=0, right=0+10=10. If P=1, left=-10, right=10. If P=2, left=-10+0=-10, right=0. There is no equilibrium index here. Correct output is -1.
[ "[-1, 2, 3, -4, 5]" ]
Explanation. Total sum = 5. P=0: left=0, right=2+3-4+5 = 6. P=1: left=-1, right=3-4+5 = 4. P=2: left=-1+2=1, right=-4+5=1. Equilibrium! Index 2 is an equilibrium index. So, the smallest is 2. My initial output was 4, but 2 is smaller. Let me correct the output for this input.
[ "[-1, 2, 3, -4, 5]" ]
Explanation. At index 2 (value 3), the sum of elements to its left (-1 + 2) is 1. The sum of elements to its right (-4 + 5) is 1. Both sums are equal. This is the first (smallest) such index found.
[ "[-1, -1, -1, -1, -1]" ]
Explanation. No equilibrium index exists in this array. The total sum is -5. For any index `P`, `2 * leftSum = -5 - nums[P]`. Since `nums[P]` is always -1, `2 * leftSum = -5 - (-1) = -4`, so `leftSum = -2`. For `P=0`, `leftSum=0`. For `P=1`, `leftSum=-1`. For `P=2`, `leftSum=-2`. Here, `P=2` works! `nums[2] = -1`. Left sum is -1 + -1 = -2. Right sum is -1 + -1 = -2. So 2 is an equilibrium index. The output should be 2.
[ "[-1, -1, -1, -1, -1]" ]
Explanation. At index 2 (value -1), the sum of elements to its left (-1 + -1) is -2. The sum of elements to its right (-1 + -1) is -2. Both sums are equal. This is the smallest such index.
Follow-up: Can you solve this problem with O(1) extra space complexity, assuming you are allowed to modify the input array or compute sums on the fly?
The input `nums` will be an array of integers. 0 <= `nums.length` <= 10^5. -1000 <= `nums[i]` <= 1000. If multiple equilibrium indices exist, return the smallest (leftmost) one. If no equilibrium index exists, return -1.
- Views
- 4