Javascript Program for Maximum equilibrium sum in an array Last Updated : 17 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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 : 4Prefix sum of arr[0..3] = Suffix sum of arr[3..6]Input : arr[] = {-2, 5, 3, 1, 2, 6, -4, 2}Output : 7Prefix sum of arr[0..3] = Suffix sum of arr[3..7]Native Approach: Checks the one-by-one given condition (prefix sum equal to suffix sum) for every element and returns the element that satisfies the given condition with maximum value. JavaScript // Javascript program to find // maximum equilibrium sum. // Function to find // maximum equilibrium sum. function findMaxSum(arr, n) { let res = -1000000000; for (let i = 0; i < n; i++) { let prefix_sum = arr[i]; for (let j = 0; j < i; j++) prefix_sum += arr[j]; let suffix_sum = arr[i]; for (let j = n - 1; j > i; j--) suffix_sum += arr[j]; if (prefix_sum == suffix_sum) res = Math.max(res, prefix_sum); } return res; } // Driver Code let arr = [-2, 5, 3, 1, 2, 6, -4, 2]; let n = arr.length; console.log(findMaxSum(arr, n)); Output7 Complexity Analysis:Time Complexity: O(n2) Auxiliary Space: O(n)Better Approach:Traverse the array and store prefix sum for each index in array presum[], in which presum[i] stores sum of subarray arr[0..i]. Do another traversal of the array and store suffix sum in another array suffsum[], in which suffsum[i] stores sum of subarray arr[i..n-1]. After this for each index check if presum[i] is equal to suffsum[i] and if they are equal then compare their value with the overall maximum so far. JavaScript // Javascript program to find // maximum equilibrium sum. // Function to find maximum // equilibrium sum. function findMaxSum(arr, n) { // Array to store prefix sum. let preSum = new Array(n); preSum.fill(0); // Array to store suffix sum. let suffSum = new Array(n); suffSum.fill(0); // Variable to store maximum sum. let ans = Number.MIN_VALUE; // Calculate prefix sum. preSum[0] = arr[0]; for (let i = 1; i < n; i++) preSum[i] = preSum[i - 1] + arr[i]; // Calculate suffix sum and compare // it with prefix sum. Update ans // accordingly. suffSum[n - 1] = arr[n - 1]; if (preSum[n - 1] == suffSum[n - 1]) ans = Math.max(ans, preSum[n - 1]); for (let i = n - 2; i >= 0; i--) { suffSum[i] = suffSum[i + 1] + arr[i]; if (suffSum[i] == preSum[i]) ans = Math.max(ans, preSum[i]); } return ans; } // Driver code let arr = [-2, 5, 3, 1, 2, 6, -4, 2]; let n = arr.length; console.log(findMaxSum(arr, n)); Output7 Complexity Analysis:Time Complexity: O(n) Auxiliary Space: O(n)Further Optimization : We can avoid the use of extra space by first computing the total sum, then using it to find the current prefix and suffix sums. JavaScript // javascript program to find // maximum equilibrium sum. // Function to find // maximum equilibrium sum. function findMaxSum(arr, n) { let sum = 0; for (let i = 0; i < n; i++) { sum = sum + arr[i]; } let prefix_sum = 0, res = Number.MIN_VALUE; for (let i = 0; i < n; i++) { prefix_sum += arr[i]; if (prefix_sum == sum) res = Math.max(res, prefix_sum); sum -= arr[i]; } return res; } // Driver Code let arr = [-2, 5, 3, 1, 2, 6, -4, 2]; let n = arr.length; console.log(findMaxSum(arr, n)); Output7 Complexity Analysis:Time Complexity: O(n) Auxiliary Space: O(1)Please refer complete article on Maximum equilibrium sum in an array for more details! Comment More infoAdvertise with us Next Article Javascript Program for Maximum equilibrium sum in an array kartik Follow Improve Article Tags : Searching JavaScript Web Technologies DSA Arrays prefix-sum +2 More Practice Tags : Arraysprefix-sumSearching Similar Reads Javascript Program for Equilibrium index of an array 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 high 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 for Maximum sum of i*arr[i] among all rotations of a given array Given an array arr[] of n integers, find the maximum that maximizes the sum of the value of i*arr[i] where i varies from 0 to n-1. Examples: Input: arr[] = {8, 3, 1, 2} Output: 29 Explanation: Lets look at all the rotations, {8, 3, 1, 2} = 8*0 + 3*1 + 1*2 + 2*3 = 11 {3, 1, 2, 8} = 3*0 + 1*1 + 2*2 + 7 min read Javascript Program for Number of pairs with maximum sum Write a javascript program for a given array arr[], count the number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j. Example: Input : arr[] = {1, 1, 1, 2, 2, 2}Output: 3Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer i 3 min read Optimizing K for Maximum Sum in Array Given an array A[] of size N. You have to choose a number K between 1 and N (inclusive). Your score will be the sum of all the elements A[i] such that i is a multiple of K, the task is to find the minimum value of K with the maximum sum. Examples: Input: N = 2, A[] = {-3, 4}Output: 2Explanation: If 6 min read Javascript Program for Queries to find maximum sum contiguous subarrays of given length in a rotating array Given an array arr[] of N integers and Q queries of the form {X, Y} of the following two types: If X = 1, rotate the given array to the left by Y positions.If X = 2, print the maximum sum subarray of length Y in the current state of the array. Examples:Â Input: N = 5, arr[] = {1, 2, 3, 4, 5}, Q = 2, 5 min read Find the maximum sum after dividing array A into M Subarrays Given a sorted array A[] of size N and an integer M. You need to divide the array A[] into M non-empty consecutive subarray (1 ? M ? N) of any size such that each element is present in exactly one of the M-subarray. After dividing the array A[] into M subarrays you need to calculate the sum [max(i) 10 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 Remove all occurrences of any element for maximum array sum Given an array of positive integers, remove all the occurrences of the element to get the maximum sum of the remaining array. Examples: Input : arr = {1, 1, 3} Output : 3 On removing 1 from the array, we get {3}. The total value is 3 Input : arr = {1, 1, 3, 3, 2, 2, 1, 1, 1} Output : 11 On removing 6 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 Like