The Wayback Machine - https://web.archive.org/web/20210929180021/https://www.geeksforgeeks.org/maximize-sum-of-selected-numbers-from-array-to-empty-it-set-2/
Skip to content
Related Articles

Related Articles

Improve Article

Maximize sum of selected numbers from Array to empty it | Set 2

  • Last Updated : 29 Sep, 2021

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 occurences 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 occurences 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:

Python3




# Python program for the above approach
  
# Function to find the maximum sum of
# selected numbers from an array to make
# the array empty
def maximizeSum(arr):
  
    # Edge Case
    if not arr:
        return 0
  
    # Stores the frequency of each element
    # in the range [0, MAX] where MAX is
    # the maximum integer in the array arr
    freq = [0] * (max(arr)+1)
  
    # Loop to iterate over array arr[]
    for i in arr:
        freq[i] += 1
  
    # Stores the DP states
    dp = [0] * (max(arr)+1)
  
    # Initially dp[1] = freq[1]
    dp[1] = freq[1]
  
    # Iterate over the range [2, MAX]
    for i in range(2, max(arr)+1):
        dp[i] = max(freq[i]*i + dp[i-2], dp[i-1])
  
    # Return Answer
    return dp[max(arr)]
  
# Driver Code
arr = [2, 2, 3, 3, 3, 4]
  
# Function Call
print(maximizeSum(arr))
Output:
9

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.

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :