Given an array of N numbers, we need to maximize the sum of selected numbers. At each step, you need to select a number Ai, delete one occurrence of it and delete all occurrences of Ai-1 and Ai+1 (if they exist) in the array. Repeat these steps until the array gets empty. The problem is to maximize the sum of the selected numbers.
Note: We have to delete all the occurrences of Ai+1 and Ai-1 elements if they are present in the array and not Ai+1 and Ai-1.
Examples:
Input : a[] = {1, 2, 3}
Output : 4
Explanation: At first step we select 1, so 1 and
2 are deleted from the sequence leaving us with 3.
Then we select 3 from the sequence and delete it.
So the sum of selected numbers is 1+3 = 4.
Input : a[] = {1, 2, 2, 2, 3, 4}
Output : 10
Explanation : Select one of the 2's from the array, so
2, 2-1, 2+1 will be deleted and we are left with {2, 2, 4},
since 1 and 3 are deleted. Select 2 in next two steps,
and then select 4 in the last step.
We get a sum of 2+2+2+4=10 which is the maximum possible.
Our aim is to maximize the sum of selected numbers. The idea is to pre-calculate the occurrence of all numbers x in the array a[].
Approach:
- Calculate the MAX value in the array.
- Create an array of size MAX and store the occurrences of each element in it.
- Since we want to maximize our answer, we will start iterating from the MAX value to 0.
- If the occurrence of the ith element is greater than 0, then add it to our answer decrease the occurrences of the i-1th element by 1, and also decrease the occurrence of ith by 1 since we have added it to our answer.
- We don’t have to decrease the occurrence of the i+1th element because we are already starting from the end so i+1th is already processed.
- There might be multiple occurrences of the ith element that’s why do not decrease i yet, to stay on the same element.
Below is the implementation of above idea:
C++
// CPP program to Maximize the sum of selected// numbers by deleting three consecutive numbers.#include <bits/stdc++.h>using namespace std;// function to maximize the sum of selected numbersint maximizeSum(int arr[], int n) { // Largest element in the array int mx = -1; for(int i = 0; i < n; i++) { mx = max(mx, arr[i]); } // An array to count the occurence of each element int freq[mx + 1]; memset(freq, 0, sizeof(freq)); for(int i = 0; i < n; i++) { freq[arr[i]]++; } // ans to store the result int ans = 0, i=mx; // Using the above mentioned approach while(i>0){ // if occurence is greater than 0 if(freq[i] > 0){ // add it to ans ans += i; // decrease i-1th element by 1 freq[i-1]--; // decrease ith element by 1 freq[i]--; }else{ // decrease i i--; } } return ans;}// Driver codeint main(){ int a[] = {1, 2, 3}; int n = sizeof(a) / sizeof(a[0]); cout << maximizeSum(a, n); return 0;} |
Java
// Java implementation of the approachimport java.util.*;import java.math.*;class GFG{ // Function to maximise the sum of selected nummbers //by deleting occurences of Ai-1 and Ai+1 public static int getMaximumSum (int arr[]) { // Number of elements in the array int n = arr.length; // Largest element in the array int max = -1; for(int i = 0; i < n; i++) { max = Math.max(max, arr[i]); } // An array to count the occurence of each element int []freq = new int[max + 1]; for(int i = 0; i < n; i++) { freq[arr[i]]++; } // ans to store the result int ans = 0, i=max; // Using the above mentioned approach while(i>0){ // if occurence is greater than 0 if(freq[i] > 0){ // add it to ans ans += i; // decrease i-1th element by 1 freq[i-1]--; // decrease ith element by 1 freq[i]--; }else{ // decrease i i--; } } return ans; } // Driver code public static void main(String[] args) { int []a = {1, 2, 3}; System.out.println(getMaximumSum(a)); }} |
Python3
# Python3 program to Maximize the sum of selected# numbers by deleting three consecutive numbers.# function to maximize the sum of# selected numbersdef maximizeSum(a, n) : # maximum in the sequence maximum = max(a) # stores the occurrences of the numbers ans = dict.fromkeys(range(0, n + 1), 0) # marks the occurrence of every # number in the sequence for i in range(n) : ans[a[i]] += 1 # ans to store the result result = 0 i = maximum # Using the above mentioned approach while i > 0: # if occurence is greater than 0 if ans[i] > 0: # add it to ans result += i; # decrease i-1th element by 1 ans[i-1] -= 1; # decrease ith element by 1 ans[i] -= 1; else: # decrease i i -= 1; return result;# Driver codeif __name__ == "__main__" : a = [1, 2, 3] n = len(a) print(maximizeSum(a, n))# This code is contributed by Ryuga |
C#
// C# implementation of the approachusing System;using System.Linq;class GFG{// Function to maximise the sum of selected nummbers//by deleting occurences of Ai-1 and Ai+1static int getMaximumSum(int []arr){ // Number of elements in the array int n = arr.Length; // Largest element in the array int max = arr.Max(); // An array to count the occurence of each element int []freq = new int[max + 1]; for(int j = 0; j < n; j++) { freq[arr[j]]++; } // ans to store the result int ans = 0, i=max; // Using the above mentioned approach while(i>0){ // if occurence is greater than 0 if(freq[i] > 0){ // add it to ans ans += i; // decrease i-1th element by 1 freq[i-1]--; // decrease ith element by 1 freq[i]--; }else{ // decrease i i--; } } return ans;}// Driver codepublic static void Main(string[] args){ int []a = {1, 2, 3}; Console.Write(getMaximumSum(a));}}// This code is contributed by rock_cool |
Output:
4
Time Complexity: Time Complexity will be the sum of (Amax + Highest occurrence of element in arr), because if the frequency is greater than 1 then we are processing that element multiple times.
-where Amax is the maximum element present in array A[].
Space Complexity: O(Amax ), where Amax is the maximum element present in array A[].
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.


