Minimum possible sum of array elements after performing the given operation | C ++ program to find the minimum possible sum of the array elements after performing the given operation ; Function to find the maximum sum of the sub array ; max_so_far represents the maximum sum found till now and max_ending_here represents the maximum sum ending at a specific index ; Iterating through the array to find the maximum sum of the subarray ; If the maximum sum ending at a specific index becomes less than 0 , then making it equal to 0. ; Function to find the minimum possible sum of the array elements after performing the given operation ; Finding the sum of the array ; Computing the minimum sum of the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double maxSubArraySum ( double a [ ] , int size ) { double max_so_far = INT_MIN , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } double minPossibleSum ( double a [ ] , int n , double x ) { double mxSum = maxSubArraySum ( a , n ) ; double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } sum = sum - mxSum + mxSum / x ; cout << setprecision ( 2 ) << sum << endl ; } int main ( ) { int N = 3 ; double X = 2 ; double A [ N ] = { 1 , -2 , 3 } ; minPossibleSum ( A , N , X ) ; }