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++
// C++ program for the above approach
#include <bits/stdc++.h>
#define ll long long
using namespace std;
// Function that finds the maximum sum
// of the array elements according to
// the given condition
ll findMaxValue(int arr[], int n)
{
// Sort the array
sort(arr, arr + n);
// Take the max value from the array
ll ans = arr[n - 1];
ll maxPossible = arr[n - 1];
// Iterate from the end of the array
for (int i = n - 2; i >= 0; --i) {
if (maxPossible > 0) {
// Check for the number had
// come before or not
if (arr[i] >= maxPossible) {
// Take the value which is
// not occurred already
ans += (maxPossible - 1);
maxPossible = maxPossible - 1;
}
else {
// Change max to new value
maxPossible = arr[i];
ans += maxPossible;
}
}
}
// Return the maximum sum
return ans;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 4, 4, 1, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << findMaxValue(arr, n);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function that finds the maximum sum
// of the array elements according to
// the given condition
static int findMaxValue(int arr[], int n)
{
// Sort the array
Arrays.sort(arr);
// Take the max value from the array
int ans = arr[n - 1];
int maxPossible = arr[n - 1];
// Iterate from the end of the array
for (int i = n - 2; i >= 0; --i)
{
if (maxPossible > 0)
{
// Check for the number had
// come before or not
if (arr[i] >= maxPossible)
{
// Take the value which is
// not occurred already
ans += (maxPossible - 1);
maxPossible = maxPossible - 1;
}
else
{
// Change max to new value
maxPossible = arr[i];
ans += maxPossible;
}
}
}
// Return the maximum sum
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 4, 4, 1, 5 };
int n = arr.length;
// Function Call
System.out.print(findMaxValue(arr, n));
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program for the above approach
# Function that finds the maximum sum
# of the array elements according to
# the given condition
def findMaxValue(arr, n):
# Sort the array
arr.sort()
# Take the max value from the array
ans = arr[n - 1]
maxPossible = arr[n - 1]
# Iterate from the end of the array
for i in range(n - 2, -1, -1):
if (maxPossible > 0):
# Check for the number had
# come before or not
if (arr[i] >= maxPossible):
# Take the value which is
# not occurred already
ans += (maxPossible - 1)
maxPossible = maxPossible - 1
else:
# Change max to new value
maxPossible = arr[i]
ans += maxPossible
# Return the maximum sum
return ans
# Driver code
if __name__=='__main__':
# Given array arr[]
arr = [ 4, 4, 1, 5 ]
n = len(arr)
# Function call
print(findMaxValue(arr,n))
# This code is contributed by rutvik_56
C#
// C# program for the above approach
using System;
class GFG{
// Function that finds the maximum sum
// of the array elements according to
// the given condition
static int findMaxValue(int []arr, int n)
{
// Sort the array
Array.Sort(arr);
// Take the max value from the array
int ans = arr[n - 1];
int maxPossible = arr[n - 1];
// Iterate from the end of the array
for (int i = n - 2; i >= 0; --i)
{
if (maxPossible > 0)
{
// Check for the number had
// come before or not
if (arr[i] >= maxPossible)
{
// Take the value which is
// not occurred already
ans += (maxPossible - 1);
maxPossible = maxPossible - 1;
}
else
{
// Change max to new value
maxPossible = arr[i];
ans += maxPossible;
}
}
}
// Return the maximum sum
return ans;
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int []arr = { 4, 4, 1, 5 };
int n = arr.Length;
// Function Call
Console.Write(findMaxValue(arr, n));
}
}
// This code is contributed by sapnasingh4991
JavaScript
<script>
// JavaScript program for the above approach
// Function that finds the maximum sum
// of the array elements according to
// the given condition
function findMaxValue(arr, n)
{
// Sort the array
arr.sort((a, b) => a - b);
// Take the max value from the array
let ans = arr[n - 1];
let maxPossible = arr[n - 1];
// Iterate from the end of the array
for (let i = n - 2; i >= 0; --i) {
if (maxPossible > 0) {
// Check for the number had
// come before or not
if (arr[i] >= maxPossible) {
// Take the value which is
// not occurred already
ans += (maxPossible - 1);
maxPossible = maxPossible - 1;
}
else {
// Change max to new value
maxPossible = arr[i];
ans += maxPossible;
}
}
}
// Return the maximum sum
return ans;
}
// Driver Code
// Given array arr[]
let arr = [ 4, 4, 1, 5 ];
let n = arr.length;
// Function Call
document.write(findMaxValue(arr, n));
// This code is contributed by Surbhi Tyagi.
</script>
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Similar Reads
Maximum Sum possible by selecting X elements from a Matrix based on given conditions Given a matrix G[][] of dimensions N Ã M, consists of positive integers, the task is to select X elements from the matrix having maximum sum considering the condition that G[i][j] can only be selected from the matrix unless all the elements G[i][k] are selected, where 0 ? k < j i.e., the jth elem
10 min read
Count of ways to choose K elements from given Array with maximum sum Given an array, arr[] of size N and an integer K, the task is to find the number of ways of selecting K array elements, such that the sum of these K elements is the maximum possible sum. Examples: Input: arr[] = {3, 1, 1, 2}, K = 3 Output: 2Explanation: The possible ways of selecting 3 elements are:
8 min read
Maximum sum obtained by dividing Array into several subarrays as per given conditions Given an array arr[] of size N, the task is to calculate the maximum sum that can be obtained by dividing the array into several subarrays(possibly one), where each subarray starting at index i and ending at index j (j>=i) contributes arr[j]-arr[i] to the sum. Examples: Input: arr[]= {1, 5, 3}, N
5 min read
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 element
6 min read
Maximize count of array elements required to obtain given sum Given an integer V and an array arr[] consisting of N integers, the task is to find the maximum number of array elements that can be selected from array arr[] to obtain the sum V. Each array element can be chosen any number of times. If the sum cannot be obtained, print -1. Examples: Input: arr[] =
8 min read