Maximize sum of selected numbers from Array to empty it | Set 2
Last Updated :
24 Mar, 2023
Given an array arr[] of N integers, the task is to maximize the sum of selected numbers over all the operations such that in each operation, choose a number Ai, delete one occurrence of it and delete all occurrences of Ai – 1 and Ai + 1 (if they exist) in the array until the array gets empty.
Examples:
Input: arr[] = {3, 4, 2}
Output: 6
Explanation: In 1st operation, select 4 and delete it. Therefore, all occurrences of 3 and 5 are deleted from arr[]. The array after the operation is arr[] = {2}. In 2nd operation select 2. Hence, the sum of all selected numbers = 4+2 = 6 which is the maximum possible.
Input: arr[] = {2, 2, 3, 3, 3, 4}
Output: 9
Explanation: In 1st operation, select 3 and delete it. Therefore, all occurrences of 2 and 4 are deleted from arr[]. The array after the operation is arr[] = {3, 3}. In 2nd and 3rd operation select 3. Hence, the sum of all selected numbers = 3+3+3 = 9, which is the maximum possible.
Approach: The given problem can be solved by counting the frequency of array elements and then find the maximum sum which is discussed in the previous post of this article.
Time Complexity: O(M + F), where M is the maximum element of the array and F is the maximum frequency of an array element.
Auxiliary Space: O(M), where M is the maximum element of the array.
Dynamic Programming Approach: The above approach can also be optimized and solved using Dynamic Programming. It can be observed that if a number Ai of the array arr[] is selected, it will contribute Ai * freq[Ai] into the final sum. Using this observation, follow the below steps to solve the given problem:
- Create an array freq[], which stores the frequency of each element in the array arr[].
- Create a 1D array dp[], where dp[i] represents the maximum possible sum of the selected values that lie in the range [1, i] in the given array.
- For each value of i, there are two possible cases as follows:
- Case 1, where i is selected. In this case, the value of dp[i] = freq[i] * i + dp[i-2].
- Case 2, where i – 1 is selected. In this case, the value of dp[i] = dp[i-1].
- Therefore, the DP relation of the above problem is:
dp[i] = max( dp[i – 1], (freq[i] * i)+ dp[i – 2]) for all values of i in range [0, MAX] where MAX is the maximum integer in arr[]
- The value stored in dp[MAX] is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximizeSum(vector< int > arr)
{
if (arr.size() == 0)
return 0;
int mx= *max_element(arr.begin(),arr.end());
int freq[mx + 1]={0};
for ( int i : arr)
freq[i] += 1;
int dp[mx + 1]={0};
dp[1] = freq[1];
for ( int i = 2; i < mx + 1; i++)
dp[i] = max(freq[i] * i + dp[i - 2],
dp[i - 1]);
return dp[mx];
}
int main()
{
vector< int > arr = {2, 2, 3, 3, 3, 4};
cout << (maximizeSum(arr));
}
|
Java
class GFG
{
static int getMax( int [] arr)
{
int max = Integer.MIN_VALUE;
for ( int i = 0 ; i < arr.length; i++)
{
if (arr[i] > max)
max = arr[i];
}
return max;
}
static int maximizeSum( int [] arr)
{
if (arr.length == 0 )
return 0 ;
int max = getMax(arr);
int [] freq = new int [max + 1 ];
for ( int i : arr)
freq[i] += 1 ;
int [] dp = new int [max + 1 ];
dp[ 1 ] = freq[ 1 ];
for ( int i = 2 ; i < max + 1 ; i++)
dp[i] = Math.max(freq[i] * i + dp[i - 2 ],
dp[i - 1 ]);
return dp[max];
}
public static void main(String [] args)
{
int [] arr = { 2 , 2 , 3 , 3 , 3 , 4 };
System.out.println((maximizeSum(arr)));
}
}
|
Python3
def maximizeSum(arr):
if not arr:
return 0
freq = [ 0 ] * ( max (arr) + 1 )
for i in arr:
freq[i] + = 1
dp = [ 0 ] * ( max (arr) + 1 )
dp[ 1 ] = freq[ 1 ]
for i in range ( 2 , max (arr) + 1 ):
dp[i] = max (freq[i] * i + dp[i - 2 ], dp[i - 1 ])
return dp[ max (arr)]
arr = [ 2 , 2 , 3 , 3 , 3 , 4 ]
print (maximizeSum(arr))
|
C#
using System;
using System.Linq;
class GFG
{
static int maximizeSum( int [] arr)
{
if (arr.Length == 0)
return 0;
int [] freq = new int [(arr.Max() + 1)];
foreach ( int i in arr) freq[i] += 1;
int [] dp = new int [(arr.Max() + 1)];
dp[1] = freq[1];
for ( int i = 2; i < arr.Max() + 1; i++)
dp[i] = Math.Max(freq[i] * i + dp[i - 2],
dp[i - 1]);
return dp[arr.Max()];
}
public static void Main()
{
int [] arr = { 2, 2, 3, 3, 3, 4 };
Console.WriteLine((maximizeSum(arr)));
}
}
|
Javascript
<script>
function maximizeSum(arr) {
if (arr.length == 0)
return 0;
let freq = new Array(Math.max(...arr) + 1).fill(0);
for (let i = 0; i < arr.length; i++)
freq[arr[i]] += 1
let dp = new Array(Math.max(...arr) + 1).fill(0);
dp[1] = freq[1]
for (let i = 2; i <= Math.max(...arr) + 1; i++)
dp[i] = Math.max(freq[i] * i + dp[i - 2], dp[i - 1])
return dp[(Math.max(...arr))]
}
let arr = [2, 2, 3, 3, 3, 4]
document.write(maximizeSum(arr))
</script>
|
Time Complexity: O(M + F), where M is the maximum element of the array.
Auxiliary Space: O(M), where M is the maximum element of the array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...