
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 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 is a list of approaches to write a Javascript program for equilibrium index of an array which we will be discussing in this article with stepwise explanation and complete example codes.
Using for Loop
In this approach for equilibrium index of an array, we have used nested for loop. The outer loop tracks the equilibrium index and two inner loops calculate the left and right sum for the comparison.
- We have declared an array arr and set a flag equiIndex to track if equilibrium index is found.
- Then we have used nested for loop where outer for loop will check for equilibrium index and two inner for loops calculates the left and right sum.
- The leftSum and rightSum are initially initialized to 0 and stores the sum of elements to the left of i and right of i respectively.
- Then we have compared leftSum and rightSum using if/else statement. If the equilibrium is found then we change the flag equiIndex to true and print the index in web console using console.log() method.
Solution
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
Here is a complete example code implementing above mentioned steps for equilibrium index of an array using for loop.
const arr = [2, 4, -3, 0, -4, 6, 1]; console.log(arr); let equiIndex = false; for (let i = 0; i < arr.length; i++) { let leftSum = 0, rightSum = 0; for (let j = 0; j < i; j++) { leftSum += arr[j]; } for (let j = i + 1; j < arr.length; j++) { rightSum += arr[j]; } if (leftSum === rightSum) { console.log(`The equilibrium index of the array is ${i}`); equiIndex = true; break; } } if (!equiIndex) { console.log(`There is no equilibrium index in the array`); }
Using Prefix Sum
In this approach for equilibrium index of an array, we have used prefix sum technique where we first create an array of cumulative sum of the elements and then compare left and right sum.
- We have declared an array arr and defined a function equiIndex() that accepts arr as argument.
- Then we have used another array prefixSum which will store the cumulative sum of the elements of the array arr.
- Then we have computed the leftSum and rightSum using prefixSum array.
- If value of leftSum and rightSum is equal, then return the index or return -1 if equilibrium index not found.
Example
Here is a complete example code implementing above mentioned steps for equilibrium index of an array using prefix sum.
let arr = [-7, 1, 5, 2, -4, 3, 0]; console.log("Given array: " +arr); function equiIndex(arr) { let n = arr.length; let prefixSum = []; prefixSum[0] = arr[0]; for (let i = 1; i < n; i++) { prefixSum[i] = prefixSum[i - 1] + arr[i]; } for (let i = 0; i < n; i++) { let leftSum = (i === 0) ? 0 : prefixSum[i - 1]; let rightSum = prefixSum[n - 1] - prefixSum[i]; if (leftSum === rightSum) { return i; } } return -1; } let result = equiIndex(arr); if (result === -1) { console.log("No equilibrium index found"); } else { console.log("Equilibrium index: ", result); }
Using Two Pointers
For equilibrium index of an array, in this approach we have used two pointers technique.
- At the beginning, we have declared an array arr, calculated the size of the array using length property and set the value of leftSum and rightSum to 0.
- Then we have used for loop to traverse the array and calculate the sum of all the elements of the array. The result flag is set to indicate that equilibrium has not been found.
- In each iteration, we subtract the current element from rightSum. This gives the sum of the elements of the right side of the i.
- Then we check if the value of leftSum and rightSum is equal. If they are equal then we return the index and break the loop as equilibrium has been found.
- If the value of leftSum and rightSum is not equal then we add current element to leftSum. It stores the sum of elements of left of i.
- At the end we check the value of flag result. The -1 represents equilibrium was not found. Value other than -1 gives the index of equilibrium.
Example
Here is a complete example code implementing above mentioned steps for Equilibrium index of an array using two pointers.
const arr = [-7, 1, 5, 2, -4, 3, 0]; console.log("Given array: " + arr); const n = arr.length; let leftSum = 0; let rightSum = 0; for (let i = 0; i < n; i++) { rightSum += arr[i]; } let result = -1; for (let i = 0; i < n; i++) { rightSum -= arr[i]; if (leftSum === rightSum) { result = i; break; } leftSum += arr[i]; } if (result === -1) { console.log("No equilibrium index found"); } else { console.log("Equilibrium index:", result); }
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^2) | O(1) |
Using Prefix Sum | O(n) | O(n) |
Using Two Pointers | O(n) | O(1) |
Conclusion
In this article to write a JavaScript program for equilibrium index of an array, we have used three different approaches. These approaches are: by using a nested for loop, prefix sum and using two pointers technique. All the approaches have time complexity of O(n) except first one.
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.