Python3 Program for Maximum equilibrium sum in an array
Last Updated :
05 Sep, 2024
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.
Python
# Python 3 program to find maximum
# equilibrium sum.
import sys
# Function to find maximum equilibrium sum.
def findMaxSum(arr, n):
res = -sys.maxsize - 1
for i in range(n):
prefix_sum = arr[i]
for j in range(i):
prefix_sum += arr[j]
suffix_sum = arr[i]
j = n - 1
while(j > i):
suffix_sum += arr[j]
j -= 1
if (prefix_sum == suffix_sum):
res = max(res, prefix_sum)
return res
# Driver Code
if __name__ == '__main__':
arr = [-2, 5, 3, 1, 2, 6, -4, 2]
n = len(arr)
print(findMaxSum(arr, n))
# This code is contributed by
# Surendra_Gangwar
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.
Python
# Python3 program to find
# maximum equilibrium sum.
# Function to find maximum
# equilibrium sum.
def findMaxSum(arr, n):
# Array to store prefix sum.
preSum = [0 for i in range(n)]
# Array to store suffix sum.
suffSum = [0 for i in range(n)]
# Variable to store maximum sum.
ans = -10000000
# Calculate prefix sum.
preSum[0] = arr[0]
for i in range(1, n):
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 i in range(n - 2, -1, -1):
suffSum[i] = suffSum[i + 1] + arr[i]
if (suffSum[i] == preSum[i]):
ans = max(ans, preSum[i])
return ans
# Driver Code
if __name__=='__main__':
arr = [-2, 5, 3, 1,2, 6, -4, 2]
n = len(arr)
print(findMaxSum(arr, n))
# This code is contributed by pratham76
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.
Python
# Python3 program to find
# maximum equilibrium sum.
import sys
# Function to find
# maximum equilibrium sum.
def findMaxSum(arr,n):
ss = sum(arr)
prefix_sum = 0
res = -sys.maxsize
for i in range(n):
prefix_sum += arr[i]
if prefix_sum == ss:
res = max(res, prefix_sum);
ss -= arr[i];
return res
# Driver code
if __name__=="__main__":
arr = [ -2, 5, 3, 1,
2, 6, -4, 2 ]
n = len(arr)
print(findMaxSum(arr, n))
# This code is contributed by rutvik_56
Time Complexity: O(n)
Auxiliary Space: O(1)
Please refer complete article on Maximum equilibrium sum in an array for more details!
Similar Reads
Python3 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]
4 min read
Python3 Program for Maximize elements using another array Given two arrays with size n, maximize the first array by using the elements from the second array such that the new array formed contains n greatest but unique elements of both the arrays giving the second array priority (All elements of second array appear before first array). The order of appeara
4 min read
Python Program to Find the 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: 29Explanation: 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 + 8*3 =
5 min read
Python3 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, Qu
4 min read
Python Program for Number of pairs with maximum sum Write a python program for a given array arr[], count 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 is 3 the
3 min read
Python Program for Maximum difference between groups of size two Given an array of even number of elements, form groups of 2 using these array elements such that the difference between the group with highest sum and the one with lowest sum is maximum.Note: An element can be a part of one group only and it has to be a part of at least 1 group. Examples: Input : ar
3 min read
Python3 Program for Size of The Subarray With Maximum Sum An array is given, find length of the subarray having maximum sum.Examples : Input : a[] = {1, -2, 1, 1, -2, 1}Output : Length of the subarray is 2Explanation: Subarray with consecutive elements and maximum sum will be {1, 1}. So length is 2Input : ar[] = { -2, -3, 4, -1, -2, 1, 5, -3 }Output : Leng
4 min read
Python Program For Stock Buy Sell To Maximize Profit The cost of a stock on each day is given in an array, find the max profit that you can make by buying and selling in those days. For example, if the given array is {100, 180, 260, 310, 40, 535, 695}, the maximum profit can earned by buying on day 0, selling on day 3. Again buy on day 4 and sell on d
5 min read
Python3 Program to Maximize count of corresponding same elements in given Arrays by Rotation Given two arrays arr1[] and arr2[] of N integers and array arr1[] has distinct elements. The task is to find the maximum count of corresponding same elements in the given arrays by performing cyclic left or right shift on array arr1[]. Examples: Input: arr1[] = { 6, 7, 3, 9, 5 }, arr2[] = { 7, 3, 9,
5 min read
Python Program to Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed Given an array, only rotation operation is allowed on array. We can rotate the array as many times as we want. Return the maximum possible summation of i*arr[i]. Examples :Â Â Input: arr[] = {1, 20, 2, 10} Output: 72 We can get 72 by rotating array twice. {2, 10, 1, 20} 20*3 + 1*2 + 10*1 + 2*0 = 72 I
3 min read