Javascript Program for Equilibrium index of an array
Last Updated :
17 Sep, 2024
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. 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.
JavaScript
// JavaScript Program to find equilibrium
// index of an array
function equilibrium(arr, n) {
let i, j;
let leftsum, rightsum;
/*Check for indexes one by one until
an equilibrium index is found*/
for (i = 0; i < n; ++i) {
/*get left sum*/
leftsum = 0;
for (let j = 0; j < i; j++)
leftsum += arr[j];
/*get right sum*/
rightsum = 0;
for (let j = i + 1; j < n; j++)
rightsum += arr[j];
/*if leftsum and rightsum are same,
then we are done*/
if (leftsum == rightsum)
return i;
}
/* return -1 if no equilibrium index is found*/
return -1;
}
// Driver code
let arr = new Array(-7, 1, 5, 2, -4, 3, 0);
n = arr.length;
console.log(equilibrium(arr, n));
Complexity Analysis:
- 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:
JavaScript
// program to find equilibrium
// index of an array
function equilibrium(arr, n) {
sum = 0; // initialize sum of whole array
leftsum = 0; // initialize leftsum
/* Find sum of the whole array */
for (let i = 0; i < n; ++i)
sum += arr[i];
for (let i = 0; i < n; ++i) {
sum -= arr[i]; // sum is now right sum for index i
if (leftsum == sum)
return i;
leftsum += arr[i];
}
/* If no equilibrium index found, then return 0 */
return -1;
}
// Driver code
arr = new Array(-7, 1, 5, 2, -4, 3, 0);
n = arr.length;
console.log("First equilibrium index is " + equilibrium(arr, n));
OutputFirst equilibrium index is 3
Complexity Analysis:
- 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.
JavaScript
// Program to find equilibrium index of an array
function equilibrium(a, n) {
if (n == 1)
return (0);
let forward = new Array(0);
let rev = new Array(0);
// Taking the prefixsum from front end array
for (let i = 0; i < n; i++) {
if (i) {
forward[i] = forward[i - 1] + a[i];
}
else {
forward[i] = a[i];
}
}
// Taking the prefixsum from back end of array
for (let i = n - 1; i > 0; i--) {
if (i <= n - 2) {
rev[i] = rev[i + 1] + a[i];
}
else {
rev[i] = a[i];
}
}
// Checking if forward prefix sum
// is equal to rev prefix
// sum
for (let i = 0; i < n; i++) {
if (forward[i] == rev[i]) {
return i;
}
}
return -1;
// If You want all the points
// of equilibrium create
// vector and push all equilibrium
// points in it and
// return the vector
}
// Driver code
arr = new Array(-7, 1, 5, 2, -4, 3, 0);
n = arr.length;
console.log("First Point of equilibrium is at index "
+ equilibrium(arr, n));
OutputFirst Point of equilibrium is at index 3
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(N)
Please refer complete article on Equilibrium index of an array for more details!
Similar Reads
Program to check if a body is in equilibrium or not Given a 2D array a[][] where each row consists of N vector coordinates of the form (xi, yi, zi) of an applied body. If the body is in equilibrium, print "YES". Otherwise, print "NO". Examples: Input: a[][] = {{4, 1, 7}, {-2, 4, -1}, {1, -5, -3}}Output: NO Input: a[][] = {{3, -1, 7}, {-5, 2, -4}, {2,
5 min read
Maximum equilibrium sum in an array Given an array arr[]. Find the maximum value of prefix sum which is also suffix sum for index i in arr[].Examples : Input : arr[] = {-1, 2, 3, 0, 3, 2, -1}Output : 4Explanation : Prefix sum of arr[0..3] = Suffix sum of arr[3..6]Input : arr[] = {-3, 5, 3, 1, 2, 6, -4, 2}Output : 7Explanation : Prefix
11 min read
Javascript Program to Find element at given index after a number of rotations An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find element at a given index.Examples : Input : arr[] : {1, 2, 3, 4, 5} ranges[] = { {0, 2}, {0, 3} } index : 1 Output : 3 Explanation
4 min read
Maximum Fixed Point (Value equal to index) in a given Array Given an array arr[] of size N, the task is to find the maximum index i such that arr[i] is equal to i. If there is no such index in the array arr[] then print -1. Examples: Input: arr[ ] = {-10, -5, 0, 3, 7}Output: 3Explanation: Only for i=3, arr[3] = 3 Input: arr[ ] = {0, 2, 5, 8, 4}Output: 4 Appr
4 min read
Javascript Program to Find the subarray with least average Given an array arr[] of size n and integer k such that k <= n.Examples : Input: arr[] = {3, 7, 90, 20, 10, 50, 40}, k = 3Output: Subarray between indexes 3 and 5The subarray {20, 10, 50} has the least average among all subarrays of size 3.Input: arr[] = {3, 7, 5, 20, -10, 0, 12}, k = 2Output: Sub
3 min read
Minimum integer to be appended to convert given Array into equilibrium Given an array A[] of length N, the task is to find the minimum integer to be appended at any end in the array to make it equilibrium. An array is in equilibrium if there exists any index i such that:sum of A[0, . . ., i-1] = sum of A[i+1, . . ., N-1] Example: Input: A[] = {5, 2, 6, 4, 3, 2} Output:
8 min read
Search for an element in a Mountain Array Given a mountain array arr[] and an integer X, the task is to find the smallest index of X in the given array. If no such index is found, print -1. Examples: Input: arr = {1, 2, 3, 4, 5, 3, 1}, X = 3Output: 2Explanation: The smallest index of X(= 3) in the array is 2. Therefore, the required output
14 min read
Javascript Program for Largest Sum Contiguous Subarray Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers that has the largest sum. Kadane's Algorithm:Initialize: max_so_far = INT_MIN max_ending_here = 0Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_f
5 min read
Maximum XOR Queries With an Element From Array Given an array arr[] of size n containing non-negative integers and also given a list of q queries in a 2D array queries[][], where each query is of the form [xi, mi]. For each query, you need to find the maximum value of (xi XOR arr[j]) such that arr[j] is less than or equal to mi.In simple terms,
15+ min read
For each Array index find the maximum value among all M operations Given an array arr[] of size N initially filled with 0 and another array Positions[] of size M, the task is to return the maximum value for each index after performing the following M operations: Make the value at index Positions[i] equal to 0All the numbers to the right of Positions[i] will be one
6 min read