C++ Program for Maximum equilibrium sum in an array
Last Updated :
31 Jan, 2022
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 : 4
Prefix sum of arr[0..3] =
Suffix sum of arr[3..6]
Input : arr[] = {-2, 5, 3, 1, 2, 6, -4, 2}
Output : 7
Prefix sum of arr[0..3] =
Suffix sum of arr[3..7]
A Simple Solution is to one by one check the given condition (prefix sum equal to suffix sum) for every element and returns the element that satisfies the given condition with maximum value.
C++
// CPP program to find
// maximum equilibrium sum.
#include <bits/stdc++.h>
using namespace std;
// Function to find
// maximum equilibrium sum.
int findMaxSum(int arr[], int n)
{
int res = INT_MIN;
for (int i = 0; i < n; i++)
{
int prefix_sum = arr[i];
for (int j = 0; j < i; j++)
prefix_sum += arr[j];
int suffix_sum = arr[i];
for (int j = n - 1; j > i; j--)
suffix_sum += arr[j];
if (prefix_sum == suffix_sum)
res = max(res, prefix_sum);
}
return res;
}
// Driver Code
int main()
{
int arr[] = {-2, 5, 3, 1,
2, 6, -4, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << findMaxSum(arr, n);
return 0;
}
Time Complexity: O(n2)
Auxiliary Space: O(n)
A Better Approach is to 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.
C++
// CPP program to find
// maximum equilibrium sum.
#include <bits/stdc++.h>
using namespace std;
// Function to find maximum
// equilibrium sum.
int findMaxSum(int arr[], int n)
{
// Array to store prefix sum.
int preSum[n];
// Array to store suffix sum.
int suffSum[n];
// Variable to store maximum sum.
int ans = INT_MIN;
// Calculate prefix sum.
preSum[0] = arr[0];
for (int 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 = max(ans, preSum[n - 1]);
for (int i = n - 2; i >= 0; i--)
{
suffSum[i] = suffSum[i + 1] + arr[i];
if (suffSum[i] == preSum[i])
ans = max(ans, preSum[i]);
}
return ans;
}
// Driver Code
int main()
{
int arr[] = { -2, 5, 3, 1,
2, 6, -4, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << findMaxSum(arr, n);
return 0;
}
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.
C++
// CPP program to find
// maximum equilibrium sum.
#include <bits/stdc++.h>
using namespace std;
// Function to find
// maximum equilibrium sum.
int findMaxSum(int arr[], int n)
{
int sum = accumulate(arr, arr + n, 0);
int prefix_sum = 0, res = INT_MIN;
for (int i = 0; i < n; i++)
{
prefix_sum += arr[i];
if (prefix_sum == sum)
res = max(res, prefix_sum);
sum -= arr[i];
}
return res;
}
// Driver Code
int main()
{
int arr[] = { -2, 5, 3, 1,
2, 6, -4, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << findMaxSum(arr, n);
return 0;
}
Time Complexity: O(n)
Auxiliary Space: O(1)
Please refer complete article on Maximum equilibrium sum in an array for more details!
Similar Reads
C++ 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
C++ Program to 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 +
6 min read
C++ Program for Number of pairs with maximum sum Given an array arr[], count number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i Example : Input : arr[] = {1, 1, 1, 2, 2, 2} Output : 3 Explanation: The maximum possible pair sum where i Method 1 (Naive)Â Traverse a loop i from 0 to n, i.e length of the array and another loop j
3 min read
Find maximum Subsequence Sum according to given conditions Given an integer array nums and an integer K, The task is to find the maximum sum of a non-empty subsequence of the array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= K is satisfied. A subsequence of an array is obtai
5 min read
Maximize frequency of an element by at most one increment or decrement of all array elements Given an array arr[] of size N, the task is to find the maximum frequency of any array element by incrementing or decrementing each array element by 1 at most once. Examples: Input: arr[] = { 3, 1, 4, 1, 5, 9, 2 } Output: 4 Explanation: Decrementing the value of arr[0] by 1 modifies arr[] to { 2, 1,
8 min read
C++ Program to Find the K-th Largest Sum Contiguous Subarray Given an array of integers. Write a program to find the K-th largest sum of contiguous subarray within the array of numbers which has negative and positive numbers. Examples:Â Input: a[] = {20, -5, -1} k = 3 Output: 14 Explanation: All sum of contiguous subarrays are (20, 15, 14, -5, -6, -1) so the
3 min read
Sum of Max of Subarrays Given an array arr[], the task is to find the sum of the maximum elements of every possible non-empty sub-arrays of the given array arr[].Examples: Input: arr[] = [1, 3, 2]Output: 15Explanation: All possible non-empty subarrays of [1, 3, 2] are {1}, {3}, {2}, {1, 3}, {3, 2} and {1, 3, 2}. The maximu
12 min read
C 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
4 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
Java Program for Equilibrium index of an array 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]
5 min read