C++ Program for Equilibrium index of an array
Last Updated :
05 Jan, 2023
Write a function int equilibrium(int[] arr, int n); that given a sequence arr[] of size n, returns an equilibrium index (if any) or -1 if no equilibrium indexes exist. The equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an array A:
Example :
Input: A[] = {-7, 1, 5, 2, -4, 3, 0}
Output: 3
3 is an equilibrium index, because:
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]
Input: A[] = {1, 2, 3}
Output: -1
Method 1 (Simple but inefficient)
Use two loops. The outer loop iterates through all the element and inner loop finds out whether the current index picked by the outer loop is equilibrium index or not.
C++
#include <bits/stdc++.h>
using namespace std;
int equilibrium( int arr[], int n)
{
int i, j;
int leftsum, rightsum;
for (i = 0; i < n; ++i)
{
leftsum = 0;
for (j = 0; j < i; j++)
leftsum += arr[j];
rightsum = 0;
for (j = i + 1; j < n; j++)
rightsum += arr[j];
if (leftsum == rightsum)
return i;
}
return -1;
}
int main()
{
int arr[] = { -7, 1, 5, 2, -4, 3, 0 };
int arr_size = sizeof (arr) / sizeof (arr[0]);
cout << equilibrium(arr, arr_size);
return 0;
}
|
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Method 2 (Tricky and Efficient)
The idea is to get the total sum of the array first. Then Iterate through the array and keep updating the left sum which is initialized as zero. In the loop, we can get the right sum by subtracting the elements one by one. Thanks to Sambasiva for suggesting this solution and providing code for this.
1) Initialize leftsum as 0
2) Get the total sum of the array as sum
3) Iterate through the array and for each index i, do following.
a) Update sum to get the right sum.
sum = sum - arr[i]
// sum is now right sum
b) If leftsum is equal to sum, then return current index.
// update leftsum for next iteration.
c) leftsum = leftsum + arr[i]
4) return -1
// If we come out of loop without returning then
// there is no equilibrium index
The image below shows the dry run of the above approach:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int equilibrium( int arr[], int n)
{
int sum = 0;
int leftsum = 0;
for ( int i = 0; i < n; ++i)
sum += arr[i];
for ( int i = 0; i < n; ++i)
{
sum -= arr[i];
if (leftsum == sum)
return i;
leftsum += arr[i];
}
return -1;
}
int main()
{
int arr[] = { -7, 1, 5, 2, -4, 3, 0 };
int arr_size = sizeof (arr) / sizeof (arr[0]);
cout << "First equilibrium index is " << equilibrium(arr, arr_size);
return 0;
}
|
Output
First equilibrium index is 3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 3 :
This is a quite simple and straightforward method. The idea is to take the prefix sum of the array twice. Once from the front end of array and another from the back end of array.
After taking both prefix sums run a loop and check for some i if both the prefix sum from one array is equal to prefix sum from the second array then that point can be considered as the Equilibrium point.
C++
#include <bits/stdc++.h>
using namespace std;
int equilibrium( int a[], int n)
{
if (n == 1)
return (0);
int forward[n] = { 0 };
int rev[n] = { 0 };
for ( int i = 0; i < n; i++) {
if (i) {
forward[i] = forward[i - 1] + a[i];
}
else {
forward[i] = a[i];
}
}
for ( int i = n - 1; i > 0; i--) {
if (i <= n - 2) {
rev[i] = rev[i + 1] + a[i];
}
else {
rev[i] = a[i];
}
}
for ( int i = 0; i < n; i++) {
if (forward[i] == rev[i]) {
return i;
}
}
return -1;
}
int main()
{
int arr[] = { -7, 1, 5, 2, -4, 3, 0 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "First Point of equilibrium is at index "
<< equilibrium(arr, n) << "
";
return 0;
}
|
Output
First Point of equilibrium is at index 3
Time Complexity: O(N)
Auxiliary Space: O(N)
Please refer complete article on Equilibrium index of an array for more details!
Share your thoughts in the comments
Please Login to comment...