Given an array arr[], the task is to obtain a non-decreasing array with the maximum sum from the given array by repeatedly decrementing array elements by 1.
Explanation:
Input: arr[] = {1, 5, 2, 3, 4}
Output: 12
Explanation: Modify the given array to {1, 2, 2, 3, 4} by reducing 5 to 2 to obtain maximum sum possible from a non-decreasing array.Input: arr[] = {1, 2, 5, 9, -3}
Output: -15
Approach: Follow the steps below to solve the problem:
- Traverse the array in reverse.
- Compare every pair of adjacent element to check if array is non-decreasing or not, i.e. check if a[i – 1] ? a[i].
- If found to be false, assign a[i – 1] = a[i].
- After completing the above steps, p[rin t the sum of the modified array.
Below is the implementation of the above approach:
C++
// C++ program for the // above approach #include <bits/stdc++.h> using namespace std;
int maximumSum(vector< int > a,
int n)
{ //Traverse the array in
// reverse
for ( int i = n - 1; i >= 0; i--)
{
//If a[i] is decreasing
if (!(a[i - 1] <= a[i]))
a[i - 1] = a[i];
}
int sum = 0;
for ( int i : a) sum += i;
//Return sum of the array
return sum;
} //Driver code int main()
{ //Given array arr[]
vector< int > arr = {1, 5, 2, 3, 4};
int N = arr.size();
cout << (maximumSum(arr, N));
} // This code is contributed by Mohit Kumar 29 |
Java
// Java program for the // above approach import java.util.*;
class GFG{
static int maximumSum( int [] a,
int n)
{ //Traverse the array in
// reverse
for ( int i = n - 1 ; i > 0 ; i--)
{
//If a[i] is decreasing
if (!(a[i - 1 ] <= a[i]))
a[i - 1 ] = a[i];
}
int sum = 0 ;
for ( int i : a) sum += i;
//Return sum of the array
return sum;
} //Driver code public static void main(String[] args)
{ //Given array arr[]
int [] arr = { 1 , 5 , 2 , 3 , 4 };
int N = arr.length;
System.out.print(maximumSum(arr, N));
} } // This code is contributed by Rajput-Ji |
Python3
# Python3 program for the # above approach def maximumSum(a, n):
# Traverse the array in reverse
for i in range (n - 1 , 0 , - 1 ):
# If a[i] is decreasing
if not a[i - 1 ] < = a[i]:
a[i - 1 ] = a[i]
# Return sum of the array
return sum (a)
# Driver Code if __name__ = = '__main__' :
arr = [ 1 , 5 , 2 , 3 , 4 ]
N = len (arr)
print (maximumSum(arr, N))
|
C#
// C# program for the // above approach using System;
class GFG{
static int maximumSum( int [] a, int n)
{ // Traverse the array in
// reverse
for ( int i = n - 1; i > 0; i--)
{
// If a[i] is decreasing
if (!(a[i - 1] <= a[i]))
a[i - 1] = a[i];
}
int sum = 0;
foreach ( int i in a) sum += i;
// Return sum of the array
return sum;
} // Driver code public static void Main(String[] args)
{ // Given array []arr
int [] arr = { 1, 5, 2, 3, 4 };
int N = arr.Length;
Console.Write(maximumSum(arr, N));
} } // This code is contributed by shikhasingrajput |
Javascript
<script> // JavaScript program for the // above approach function maximumSum(a,n)
{ //Traverse the array in
// reverse
for (let i = n - 1; i > 0; i--)
{
//If a[i] is decreasing
if (!(a[i - 1] <= a[i]))
a[i - 1] = a[i];
}
let sum = 0;
for (let i=0;i< a.length;i++) sum += a[i];
//Return sum of the array
return sum;
} //Driver code let arr=[1, 5, 2, 3, 4]; let N = arr.length; document.write(maximumSum(arr, N)); // This code is contributed by patel2127 </script> |
Output:
12
Time Complexity: O(N)
Auxiliary Space: O(1)