JavaScript Program for Maximum equilibrium sum in an array


The equilibrium sum of the given array is the sum at a particular point or index of the array after which the subarray has the total sum equal to the sum of the subarray starting from the 0th index to the current index (including the current index). We will see the examples and the code implementations in JavaScrript with the different approaches.

Introduction to Problem

We are given a single array in which we have to find a point from which the total sum of the elements that are present on the left side (including the current index element) is equal to the sum of the elements on the right side (excluding the current index element). There could be more than one such type of index present in which the property of equilibrium sum is present, for those cases we have to return the maximum sum present.

For example

The given array is: 2, 3, -1, 0, -1, 2, 3
In the above array, the maximum equilibrium sum is 4 which can be found at both indexes 2 and 3. 

We will see the three approaches to find the maximum equilibrium sum.

Naive Approach

In this approach, we will traverse over the array using nested for loops −

// function to get the equilibrium sum
function equiSum(arr){

   // getting the length of the array 
   var n = arr.length 
   
   // initializing the answer 
   var ans = 0 
   
   // traversing over the array 
   for(var i =0; i<n; i++)    {
      var sum1 = 0;
      
      // getting sum upto the current index 
      for(var j =0; j<= i; j++)        {
         sum1 += arr[j]
      }
      var sum2 = 0;
      
      // getting sum of elements present after the current element 
      for(var j = i+1; j<n; j++) {
         sum2 += arr[j];
      }
      if(sum1 == sum2 && ans < sum1)        { 
         ans = sum1  
      }
   }
   console.log("The maximum equilibrium sum in the given array is: " + ans);
}

// defining the array 
arr = [2, 3, -1, 0, -1, 2, 3]
equiSum(arr)

Time and Space Complexity

The time complexity of the above code is O(N*N), where N is the size of the given array as we are traversing over the each element of the given array for each iteration.

The space complexity of the above code is O(1), as we are not using the extra space.

Prefix and Suffix Array Approach

In this approach we will maintain the suffix and the prefix array and based on them we will try to find the answer.

Example

// function to get the equilibrium sum
function equiSum(arr){

   // getting the length of the array 
   var n = arr.length 
   
   // initializing the answer 
   var ans = 0 
   
   // traversing over the array to get the prefix sum 
   var preSum = new Array(n)
   preSum[0] = arr[0];
   for(var i =1; i<n; i++) {
      preSum[i] = preSum[i-1] + arr[i];
   }
   var sufSum = new Array(n)
   sufSum[0] = arr[n-1];
   for(var i = n-2; i>=0;i--){
      sufSum[n-i-1] = sufSum[n-i-2] + arr[i];
   }
   
   // traversing over the array 
   for(var i = 0; i<n-1; i++){
      if(preSum[i] == sufSum[n-i-2] && ans < preSum[i]){
         ans = preSum[i];
      }
   }
   console.log("The maximum equilibrium sum in the given array is: " + ans);
}

// defining the array 
arr = [2, 3, -1, 0, -1, 2, 3]
equiSum(arr)

Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the given array as we are traversing over each element of the given array only three times.

The space complexity of the above code is O(N), as we are storing the prefix and suffix sum in the arrays of size N.

Efficient Approach

In this approach, we will maintain two variables and using them we will try to get the required answer.

// function to get the equilibrium sum
function equiSum(arr){

   // getting the length of the array 
   var n = arr.length 
   
   // initializing the answer 
   var ans = 0 
   
   // traversing over the array to get the total sum 
   var total_sum = 0
   for(var i =0; i<n; i++) {
      total_sum += arr[i]
   }
   
   // traversing over the array 
   var cur_sum = 0;
   for(var i = 0; i<n-1; i++) {
      cur_sum += arr[i];
      total_sum -= arr[i];
      if(cur_sum == total_sum && ans < cur_sum) {
         ans = cur_sum
      }
   }
   console.log("The maximum equilibrium sum in the given array is: " + ans);
}

// defining the array 
arr = [2, 3, -1, 0, -1, 2, 3]
equiSum(arr)

Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the given array as we are traversing over each element of the given array only two times.

The space complexity of the above code is O(1), as we are not using any extra space.

Conclusion

In the above tutorial, we have implemented a JavaScript program for maximum equilibrium sum in an array. Equilibrium sum is the total sum of the elements that are present on the left side (including the current index element) and is equal to the sum of the elements on the right side (excluding the current index element). We have seen three approaches with the time and space complexity: Naive approach O(N*N) & O(1), Prefix Sum O(N) & O(N), and Efficient approach O(N) & O(1).

Updated on: 14-Apr-2023

757 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements