Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Equilibrium Index of an Array
To write a JavaScript program for equilibrium index of an array, we are going to discuss three different approaches with detailed explanation and example codes. The equilibrium index of an array is the position where the sum of the elements to its left and right equals one another.
In this article we are having an array of integers, our task is to write a JavaScript program for equilibrium index of an array.
Example
Input: arr = [-7, 1, 5, 2, -4, 3, 0] Output: Equilibrium index: 3 At index 3, left sum = -7 + 1 + 5 = -1 right sum = -4 + 3 + 0 = -1 left sum = right sum
Approaches for Equilibrium Index of Array
Here are three approaches to write a JavaScript program for equilibrium index of an array:
Using for Loop
In this approach for equilibrium index of an array, we use nested for loops. The outer loop tracks the equilibrium index and two inner loops calculate the left and right sum for the comparison.
- We declare an array arr and set a flag equiIndex to track if equilibrium index is found.
- Use nested for loops where outer loop checks for equilibrium index and two inner loops calculate the left and right sum.
- The leftSum and rightSum are initially set to 0 and store the sum of elements to the left and right of index i respectively.
- Compare leftSum and rightSum using if/else statement. If equilibrium is found, change the flag and print the index using console.log() method.
How It Works
arr = [2, 4, -3, 0, -4, 6, 1]; For i=0, leftSum = 0 rightSum = a[1]+a[2]+a[3]+a[4]+a[5]+a[6] = 4+(-3)+0+(-4)+6+1 = 4 => leftSum != rightSum For i=1, leftSum = a[0] = 2 rightSum = a[2]+a[3]+a[4]+a[5]+a[6] = -3+0+(-4)+6+1 = 0 => leftSum != rightSum For i=2, leftSum = a[0]+a[1] = 2+4 = 6 rightSum = a[3]+a[4]+a[5]+a[6] = 0+(-4)+6+1 = 3 => leftSum != rightSum For i=3, leftSum = a[0]+a[1]+a[2] = 2+4+(-3) = 3 rightSum = a[4]+a[5]+a[6] = -4+6+1 = 3 => leftSum = rightSum, loop breaks. => Equilibrium index: 3
Example
const arr = [2, 4, -3, 0, -4, 6, 1];
console.log("Array:", arr);
let equiIndex = false;
for (let i = 0; i
Array: 2,4,-3,0,-4,6,1
The equilibrium index of the array is 3
Using Prefix Sum
In this approach for equilibrium index of an array, we use prefix sum technique where we first create an array of cumulative sum of the elements and then compare left and right sum.
- We declare an array arr and define a function equiIndex() that accepts arr as argument.
- Use another array prefixSum which stores the cumulative sum of the elements of the array arr.
- Compute the leftSum and rightSum using prefixSum array.
- If value of leftSum and rightSum is equal, return the index or return -1 if equilibrium index not found.
Example
let arr = [-7, 1, 5, 2, -4, 3, 0];
console.log("Given array:", arr);
function equiIndex(arr) {
let n = arr.length;
let prefixSum = [];
// Build prefix sum array
prefixSum[0] = arr[0];
for (let i = 1; i
Given array: -7,1,5,2,-4,3,0
Equilibrium index: 3
Using Two Pointers
For equilibrium index of an array, in this approach we use two pointers technique that maintains running sums from both sides.
- Declare an array arr, calculate the size using length property and set leftSum and rightSum to 0.
- Use for loop to traverse the array and calculate the sum of all elements. The result flag is set to indicate equilibrium has not been found.
- In each iteration, subtract the current element from rightSum. This gives the sum of elements on the right side of index i.
- Check if leftSum and rightSum are equal. If they are equal, return the index and break the loop as equilibrium has been found.
- If leftSum and rightSum are not equal, add current element to leftSum. It stores the sum of elements to the left of i.
- At the end check the value of flag result. The -1 represents equilibrium was not found.
Example
const arr = [-7, 1, 5, 2, -4, 3, 0];
console.log("Given array:", arr);
const n = arr.length;
let leftSum = 0;
let rightSum = 0;
// Calculate total sum
for (let i = 0; i
Given array: -7,1,5,2,-4,3,0
Equilibrium index: 3
Complexity Comparison
Here is a comparison of time and space complexity of all the above approaches.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Using for Loop | O(n²) | O(1) |
| Using Prefix Sum | O(n) | O(n) |
| Using Two Pointers | O(n) | O(1) |
Conclusion
In this article, we explored three approaches to find the equilibrium index of an array in JavaScript. The two-pointers technique is the most optimal with O(n) time complexity and O(1) space complexity.
Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
