Maximum modified Array sum possible by choosing elements as per the given conditions
Last Updated :
30 Jun, 2022
Given an array arr[] of size N, the task is to find the maximum possible sum of the array elements such that the element can be chosen as per the below conditions:
- For each index i the value of the element is arr[i], then we can add any element from 1 to min(N, arr[i]) into the sum or else leave that element.
- If some element is already added to the sum, then we can not add the same element again to the sum.
Examples:
Input: arr[] = {1, 2, 2, 4}
Output: 7
Explanation:
Initially, the total sum is 0.
Sum after adding element at index 1 to sum = 1. Elements added to sum = {1}
Sum after adding element at index 2 to sum = 3. Elements added to sum = {1, 2}
Now, coming at index 3 we can not add any element from (1 to 2) to the sum because we have already added the elements {1, 2} into the sum. So, leave the element at index 3.
Then add the element at index 4 to sum since 4 was not added to sum before therefore the maximum sum we can get is 3+4=7.
Input: arr[] = {4, 4, 1, 5}
Output: 13
Approach: This problem can be solved using Greedy Technique. Below are the steps:
- Sort the elements of the array in ascending order.
- Maintain the maximum possible number (say maxPossible) which can be added to the total sum.
- Initialise maxPossible with the maximum element of the array and add it to the total sum.
- Traverse the array from the end to the beginning and check if the given element value has been added to the total sum before or not.
- If the element value has been added before then we will add (element – 1) to the total sum and if the element value is less than maxPossible then we will add that number and change the maxPossible to the element value.
- Print the value of total sum after the above steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll findMaxValue( int arr[], int n)
{
sort(arr, arr + n);
ll ans = arr[n - 1];
ll maxPossible = arr[n - 1];
for ( int i = n - 2; i >= 0; --i) {
if (maxPossible > 0) {
if (arr[i] >= maxPossible) {
ans += (maxPossible - 1);
maxPossible = maxPossible - 1;
}
else {
maxPossible = arr[i];
ans += maxPossible;
}
}
}
return ans;
}
int main()
{
int arr[] = { 4, 4, 1, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findMaxValue(arr, n);
}
|
Java
import java.util.*;
class GFG{
static int findMaxValue( int arr[], int n)
{
Arrays.sort(arr);
int ans = arr[n - 1 ];
int maxPossible = arr[n - 1 ];
for ( int i = n - 2 ; i >= 0 ; --i)
{
if (maxPossible > 0 )
{
if (arr[i] >= maxPossible)
{
ans += (maxPossible - 1 );
maxPossible = maxPossible - 1 ;
}
else
{
maxPossible = arr[i];
ans += maxPossible;
}
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 4 , 4 , 1 , 5 };
int n = arr.length;
System.out.print(findMaxValue(arr, n));
}
}
|
Python3
def findMaxValue(arr, n):
arr.sort()
ans = arr[n - 1 ]
maxPossible = arr[n - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
if (maxPossible > 0 ):
if (arr[i] > = maxPossible):
ans + = (maxPossible - 1 )
maxPossible = maxPossible - 1
else :
maxPossible = arr[i]
ans + = maxPossible
return ans
if __name__ = = '__main__' :
arr = [ 4 , 4 , 1 , 5 ]
n = len (arr)
print (findMaxValue(arr,n))
|
C#
using System;
class GFG{
static int findMaxValue( int []arr, int n)
{
Array.Sort(arr);
int ans = arr[n - 1];
int maxPossible = arr[n - 1];
for ( int i = n - 2; i >= 0; --i)
{
if (maxPossible > 0)
{
if (arr[i] >= maxPossible)
{
ans += (maxPossible - 1);
maxPossible = maxPossible - 1;
}
else
{
maxPossible = arr[i];
ans += maxPossible;
}
}
}
return ans;
}
public static void Main(String[] args)
{
int []arr = { 4, 4, 1, 5 };
int n = arr.Length;
Console.Write(findMaxValue(arr, n));
}
}
|
Javascript
<script>
function findMaxValue(arr, n)
{
arr.sort((a, b) => a - b);
let ans = arr[n - 1];
let maxPossible = arr[n - 1];
for (let i = n - 2; i >= 0; --i) {
if (maxPossible > 0) {
if (arr[i] >= maxPossible) {
ans += (maxPossible - 1);
maxPossible = maxPossible - 1;
}
else {
maxPossible = arr[i];
ans += maxPossible;
}
}
}
return ans;
}
let arr = [ 4, 4, 1, 5 ];
let n = arr.length;
document.write(findMaxValue(arr, n));
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...