Find the Maximum possible Sum for the given conditions
Given an array arr[] of size N, the task is to find the maximum possible sum of the array by following the given conditions:
- At every step, only one element can be used to increase the sum.
- If some element K is selected from the array, the remaining numbers in the array get reduced by one.
- The elements in the array can’t be reduced past 0.
Examples:
Input: arr = {6, 6, 6}
Output: 15
Explanation:
Initially, the total sum is 0. Since all the elements are equal, any one element is chosen.
Sum after choosing the first six = 6. Remaining elements = {5, 5}.
Sum after choosing the five = 11. Remaining elements = {4}.
Finally, four is chosen making the maximum sum 15.
Input: arr = {0, 1, 0}
Output: 1
Explanation:
Initially, the total sum is 0. There is only one number that can be chosen in the array because rest of the elements are 0.
Therefore, the maximum sum = 1.
Approach: Since the value of all the other elements gets reduced by 1, clearly, we get the maximum sum if we choose the maximum element at every iteration. Therefore, in order to do this, sorting is used.
- The idea is to sort the elements of the array in descending order.
- Now, since we get to choose the maximum value at every iteration, we calculate the value of the element K at some index ‘i’ as (K – i).
- If at any index the value of the element becomes 0, then all the elements past that index will be 0.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int maxProfit( int arr[], int n)
{
sort(arr, arr + n, greater< int >());
int ans = 0;
for ( int i = 0; i < n; i++)
{
if ((arr[i] - (1 * i)) > 0)
ans += (arr[i] - (1 * i));
if ((arr[i] - (1 * i)) == 0)
break ;
}
return ans;
}
int main()
{
int arr[] = {6, 6, 6};
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxProfit(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Collections;
public class GFG{
static int maxProfit(Integer [] arr)
{
Arrays.sort(arr, Collections.reverseOrder());
int ans = 0 ;
for ( int i = 0 ; i < arr.length; i++)
{
if ((arr[i] - ( 1 * i)) > 0 )
ans += (arr[i] - ( 1 * i));
if ((arr[i] - ( 1 * i)) == 0 )
break ;
}
return ans;
}
public static void main(String[] args)
{
Integer arr[] = { 6 , 6 , 6 };
System.out.println(maxProfit(arr));
}
}
|
Python3
def maxProfit(arr):
arr.sort(reverse = True )
ans = 0
for i in range ( len (arr)):
if (arr[i] - ( 1 * i))> 0 :
ans + = (arr[i] - ( 1 * i))
if (arr[i] - ( 1 * i)) = = 0 :
break
return ans
if __name__ = = "__main__" :
arr = [ 6 , 6 , 6 ]
print (maxProfit(arr))
|
C#
using System;
class GFG{
static int maxProfit( int [] arr)
{
Array.Sort(arr);
Array.Reverse(arr);
int ans = 0;
for ( int i = 0; i < arr.Length; i++)
{
if ((arr[i] - (1 * i)) > 0)
ans += (arr[i] - (1 * i));
if ((arr[i] - (1 * i)) == 0)
break ;
}
return ans;
}
static public void Main ()
{
int [] arr = { 6, 6, 6 };
Console.Write(maxProfit(arr));
}
}
|
Javascript
<script>
function maxProfit(arr)
{
arr.sort();
arr.reverse();
let ans = 0;
for (let i = 0; i < arr.length; i++)
{
if ((arr[i] - (1 * i)) > 0)
ans += (arr[i] - (1 * i));
if ((arr[i] - (1 * i)) == 0)
break ;
}
return ans;
}
let arr = [ 6, 6, 6 ];
document.write(maxProfit(arr));
</script>
|
Time Complexity: O(n*log(n))
Auxiliary Space: O(1)
Last Updated :
28 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...