Reduce given Array to 0 by maximising sum of chosen elements
Given an array arr[] containing N positive integers, the task is to maximize the array sum when after every sum operation all the remaining array elements decrease by 1.
Note: The value of an array element does not go below 0.
Examples:
Input: arr[] = {6, 2, 4, 5}
Output: 12
Explanation: Add 6 initially to the final sum.
The final sum becomes 6 and the remaining array elements {1, 3, 4}.
Add 3 with the sum. The sum becomes 9 and the remaining elements {0, 3}
Add 3 with the sum. The sum becomes 12 and only 0 remains in the array.
Add 0 with the sum. The sum remains unchanged.
Input: arr[] = {5, 6, 4}
Output: 12
Naive approach: Find the maximum element from the array. Add it to the sum and decrease all other elements by 1 and change the current element to 0 so it is not repeated again in the loop. Do this process until all the elements become 0.
C++
#include <bits/stdc++.h>
using namespace std;
int maxSum( int arr[], int N)
{
int ans = 0;
while (1) {
int maxValueIndex = max_element(arr, arr + N) - arr;
if (arr[maxValueIndex] <= 0)
break ;
ans += arr[maxValueIndex];
arr[maxValueIndex] = 0;
for ( int i = 0; i < N; i++) {
arr[i]--;
}
}
return ans;
}
int main()
{
int arr[] = { 6, 2, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxSum(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maxSum( int arr[], int N)
{
int ans = 0 ;
while ( true ) {
int maxValue = Arrays.stream(arr).max().getAsInt();
;
int maxValueIndex = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] == maxValue) {
maxValueIndex = i;
break ;
}
}
if (arr[maxValueIndex] <= 0 )
break ;
ans += arr[maxValueIndex];
arr[maxValueIndex] = 0 ;
for ( int i = 0 ; i < N; i++) {
arr[i]--;
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 6 , 2 , 4 , 5 };
int N = arr.length;
System.out.print(maxSum(arr, N));
}
}
|
Python3
def maxSum(arr, N):
ans = 0
while ( 1 ):
maxValueIndex = arr.index( max (arr))
if (arr[maxValueIndex] < = 0 ):
break
ans + = arr[maxValueIndex]
arr[maxValueIndex] = 0
for i in range ( 0 , N):
arr[i] - = 1
return ans
arr = [ 6 , 2 , 4 , 5 ]
N = len (arr)
print (maxSum(arr, N))
|
C#
using System;
using System.Linq;
class GFG
{
static int maxSum( int [] arr, int N)
{
int ans = 0;
while ( true ) {
int maxValue = arr.Max();
int maxValueIndex = 0;
for ( int i = 0; i < arr.Length; i++) {
if (arr[i] == maxValue) {
maxValueIndex = i;
break ;
}
}
if (arr[maxValueIndex] <= 0)
break ;
ans += arr[maxValueIndex];
arr[maxValueIndex] = 0;
for ( int i = 0; i < N; i++) {
arr[i]--;
}
}
return ans;
}
public static int Main()
{
int [] arr = { 6, 2, 4, 5 };
int N = arr.Length;
Console.Write(maxSum(arr, N));
return 0;
}
}
|
Javascript
<script>
function maxSum(arr, N)
{
let ans = 0;
while (1) {
let maxValueIndex = arr.indexOf(Math.max(...arr));
if (arr[maxValueIndex] <= 0)
break ;
ans += arr[maxValueIndex];
arr[maxValueIndex] = 0;
for (let i = 0; i < N; i++) {
arr[i]--;
}
}
return ans;
}
let arr = [ 6, 2, 4, 5 ];
let N = arr.length;
document.write(maxSum(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1), since no extra space has been taken.
Efficient Approach: The solution of the problem is based on the concept of sorting. As the values decrease in each step, the higher values should be added first with the final sum. Follow the steps mentioned below to solve the problem:
- Sort the array in descending order.
- Run a loop from 1 to N.
- For each index i the value added to the final sum will be (arr[i] – i) as it will be added after i decrement of the value.
- As the sum value cannot go below 0, add max(arr[i]-i, 0) to the final sum.
- Return the final sum.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int maxSum( int arr[], int N)
{
int ans = 0;
sort(arr, arr + N, greater< int >());
for ( int i = 0; i < N; i++) {
int value = arr[i];
int current = max(0, value - i);
ans = ans + current;
}
return ans;
}
int main()
{
int arr[] = { 6, 2, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxSum(arr, N);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
public static void reverse( int [] array)
{
int n = array.length;
for ( int i = 0 ; i < n / 2 ; i++) {
int temp = array[i];
array[i] = array[n - i - 1 ];
array[n - i - 1 ] = temp;
}
}
static int maxSum( int [] arr, int N)
{
int ans = 0 ;
Arrays.sort(arr);
reverse(arr);
for ( int i = 0 ; i < N; i++) {
int value = arr[i];
int current = Math.max( 0 , value - i);
ans = ans + current;
}
return ans;
}
public static void main(String args[])
{
int [] arr = { 6 , 2 , 4 , 5 };
int N = arr.length;
System.out.println(maxSum(arr, N));
}
}
|
Python
def maxSum(arr, N):
ans = 0
arr.sort(reverse = True )
for i in range (N):
value = arr[i]
current = max ( 0 , value - i)
ans = ans + current
return ans
if __name__ = = "__main__" :
arr = [ 6 , 2 , 4 , 5 ]
N = len (arr)
print (maxSum(arr, N))
|
C#
using System;
class GFG
{
static int maxSum( int [] arr, int N)
{
int ans = 0;
Array.Sort< int >(
arr, delegate ( int m, int n) { return n - m; });
for ( int i = 0; i < N; i++) {
int value = arr[i];
int current = Math.Max(0, value - i);
ans = ans + current;
}
return ans;
}
public static int Main()
{
int [] arr = { 6, 2, 4, 5 };
int N = arr.Length;
Console.Write(maxSum(arr, N));
return 0;
}
}
|
Javascript
<script>
function maxSum(arr, N)
{
let ans = 0;
arr.sort( function (a, b) { return b - a })
for (let i = 0; i < N; i++) {
let value = arr[i];
let current = Math.max(0, value - i);
ans = ans + current;
}
return ans;
}
let arr = [6, 2, 4, 5];
let N = arr.length;
document.write(maxSum(arr, N));
</script>
|
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Last Updated :
06 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...