Equilibrium Index

64.6k
0

Given an integer array nums, return the first index where the sum of all elements on the left equals the sum of all elements on the right.

Both sums exclude the element at the equilibrium index. Return -1 if no equilibrium index exists.

Example 1

Input: nums = [-7, 1, 5, 2, -4, 3, 0]

Output: 3

Explanation: At index 3, the left-side sum is -7 + 1 + 5 = -1, and the right-side sum is -4 + 3 + 0 = -1. Both sums are equal, so index 3 is the equilibrium index.

Example 2

Input: nums = [1, 2, 3]

Output: -1

Explanation: There is no index where the left-side sum equals the right-side sum.

Brute Force Approach

The most direct idea checks every index as a possible equilibrium position.

For each selected index, calculate the complete sum on the left and the complete sum on the right separately. This follows the definition directly, but it adds many of the same elements again when checking different indices.

Approach

  1. Store the array size in n. If n == 0, return -1 because an empty array contains no valid index.

  2. Traverse every index from 0 to n - 1, treating the current position as a possible equilibrium index.

  3. Initialise leftSum to 0 and add all elements before the current index.

  4. Initialise rightSum to 0 and add all elements after the current index.

  5. Compare leftSum and rightSum. Return the current index immediately when both are equal so that the first equilibrium index is returned.

  6. Return -1 if the sums never match.

Dry run

Equilibrium Index

Equilibrium Index

Solution

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Return the first equilibrium index, or -1 if none exists.
int equilibriumIndex(vector<int>& nums) {
int n = nums.size();
if (n == 0) {
return -1;
}
for (int index = 0; index < n; index++) {
long long leftSum = 0;
long long rightSum = 0;
// Sum the elements before the current index.
for (int left = 0; left < index; left++) {
leftSum += nums[left];
}
// Sum the elements after the current index.
for (int right = index + 1; right < n; right++) {
rightSum += nums[right];
}
if (leftSum == rightSum) {
return index;
}
}
return -1;
}
};
int main() {
vector<int> nums = {1, 7, 3, 6, 5, 6};
Solution solution;
cout << solution.equilibriumIndex(nums) << endl; // Prints 3.
return 0;
}

Complexity

Time Complexity: O(N²), where N is the array size. Checking a single index can require summing all other elements.

Space Complexity: O(1). The algorithm uses a fixed number of variables for the indices and sums.

Better Approach

The Brute Force Approach repeatedly recalculates the left-side and right-side sums. When we move from one index to the next, only one element is added to the left side. We can reuse the previous sum and add that element to calculate the new sum. The same idea works for the right side when we scan from right to left.

Use two helper arrays to store these sums. leftSum[index] contains the sum of all elements before index, and rightSum[index] contains the sum of all elements after it. Both sums exclude nums[index].

For example, in [1, 7, 3, 6, 5, 6], leftSum[3] is leftSum[2] + nums[2] = 8 + 3 = 11. Similarly, rightSum[3] is rightSum[4] + nums[4] = 6 + 5 = 11. Since the two sums match, index 3 is an equilibrium index. Once both arrays are built, each index takes just one comparison to check.

Approach

  1. Store the array size in n. If the array is empty, return -1.

  2. Create two arrays of size n. Use leftSum[index] to store the sum before index and rightSum[index] to store the sum after it.

  3. Set leftSum[0] to 0 because there are no elements before the first position. Scan from left to right and calculate leftSum[index] = leftSum[index - 1] + nums[index - 1].

  4. Set rightSum[n - 1] to 0 because there are no elements after the last position. Scan from right to left and calculate rightSum[index] = rightSum[index + 1] + nums[index + 1].

  5. Compare the two sums at each index from left to right. Return the first index where leftSum[index] == rightSum[index].

  6. Return -1 if the sums never match.

Dry run

Equilibrium Index Better

Equilibrium Index Better

Solution

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Return the first index with equal sums on both sides, or -1 if none exists.
int equilibriumIndex(vector<int>& nums) {
int n = nums.size();
// An empty array has no valid index.
if (n == 0) {
return -1;
}
// The first index has no left side, and the last has no right side.
vector<long long> leftSum(n, 0);
vector<long long> rightSum(n, 0);
// Build left sums by adding the element just before each index.
for (int index = 1; index < n; index++) {
leftSum[index] = leftSum[index - 1] + nums[index - 1];
}
// Build right sums by adding the element just after each index.
for (int index = n - 2; index >= 0; index--) {
rightSum[index] = rightSum[index + 1] + nums[index + 1];
}
// Scan from left to right and return the first match.
for (int index = 0; index < n; index++) {
if (leftSum[index] == rightSum[index]) {
return index;
}
}
return -1;
}
};
int main() {
vector<int> nums = {1, 7, 3, 6, 5, 6};
Solution solution;
cout << solution.equilibriumIndex(nums) << endl; // Prints 3.
return 0;
}

Complexity

Time Complexity : O(N). Three linear traversals build the left sums, build the right sums, and find the first matching index.

Space Complexity : O(N). Two arrays of size N store the left-side and right-side sums.

Optimal Approach

The total sum of the array includes the elements on the left, the current element, and the elements on the right. Once we know the total, we can calculate the right-side sum by subtracting the current element and the running left-side sum.

Start with a left-side sum of 0. At each index, calculate the right-side sum and compare it with the left-side sum. After the comparison, add the current element to leftSum so it becomes part of the left side for the next index.

Approach

  1. Store the array size in n. If n == 0, return -1.

  2. Calculate totalSum by adding every array element.

  3. Initialise leftSum to 0 because there are no elements before index 0.

  4. Traverse the array from left to right. At each index, calculate rightSum = totalSum - leftSum - nums[index].

  5. If leftSum == rightSum, return the current index. Otherwise, add nums[index] to leftSum before moving to the next index.

  6. Return -1 if the sums never match.

Dry run

Equilibrium Index Optimal

Equilibrium Index Optimal

Solution

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Return the first index with equal sums on both sides, or -1 if none exists.
int equilibriumIndex(vector<int>& nums) {
int n = nums.size();
// An empty array has no valid index.
if (n == 0) {
return -1;
}
long long totalSum = 0;
// Compute the total once so each right sum can be found directly.
for (int value : nums) {
totalSum += value;
}
long long leftSum = 0;
for (int index = 0; index < n; index++) {
// Subtract the left sum and current value to get the right sum.
long long rightSum = totalSum - leftSum - nums[index];
if (leftSum == rightSum) {
return index;
}
// Add the current value to the left sum for the next index.
leftSum += nums[index];
}
return -1;
}
};
int main() {
vector<int> nums = {1, 7, 3, 6, 5, 6};
Solution solution;
cout << solution.equilibriumIndex(nums) << endl; // Prints 3.
return 0;
}

Complexity

Time Complexity: O(N). One traversal calculates totalSum, and a second traversal checks the indices.

Space Complexity: O(1). The algorithm uses a fixed number of variables for the indices and sums.

Interview follow-up Questions

The equilibrium condition compares the elements before the selected index with the elements after it.

ArraysTCS NQT

Read Similar Blogs

Comments0