Given an array **arr[]**, the task is to find the maximum score of deleting an element where each element of the array can be deleted with the score of the element, but the constraint is if we delete **arr[i]**, then **arr[i] + 1** and **arr[i] – 1** is gets automatically deleted with 0 scores.**Examples:**

Input:arr[] = {7, 2, 1, 8, 3, 3, 6, 6}Output:27Explanation:Step 0:arr[] = 7 2 1 8 336 6,Score:0Step 1:arr[] = 7 1 836 6,Score:3Step 2:arr[] = 718 6 6,Score:6Step 3:arr[] = 7 866,Score:7Step 4:arr[] = 86,Score:13Step 5:arr[] =8Score:19Step 6:arr[] = []Score:27Input:arr[] = 1 2 3Output:4

**Approach:** The idea is to use Dynamic Programming to solve this problem. The key observation of the problem is that for removing any element from the array the occurrence of the element and the value itself is the important factor.

Let’s take an example to understand the observation if the sequence was 4 4 5. Then, we have two choices to choose from 4 to 5. Now, on choosing 4, his score would be 4*2 = 8. On the other hand, if we choose 5, his score would be 5*1 = 5. Clearly, the maximal score is 8.

Hence, for the above sequence 4 4 5, freq[4] = 2, and freq[5] = 1.

Finally, to find the optimal score, it would be easy to first break the problem down into a smaller problem. In this case, we break the sequence into smaller sequences and find an optimal solution for it. For the sequence of numbers containing only 0, the answer would be 0. Similarly, if a sequence contains only the number 0 and 1, then the solution would be count[1]*1.**Recurrence Relation:**

dp[i] = max(dp[i – 1], dp[i – 2] + i*freq[i])

Basically, we have 2 cases, either to pick an **i ^{th}** element and other is not to pick an

**i**element.

^{th}**Case 1:** If we pick the ith element, Maximum score till the ith element will be dp[i-2] + i*freq[i] (choosing ith element means deleting (i-1)th element) **Case 2:** If we don’t pick the ith element, the Maximum Score till ith element will dp[i-1]

Now as We have to maximize the score, We will take the maximum of both.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// maximum score of the deleting a ` `// element from an array ` ` ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function to find the maximum ` `// score of the deleting an element ` `// from an array ` `int` `findMaximumScore(vector<` `int` `> a, ` `int` `n) ` `{ ` ` ` ` ` `// Creating a map to keep ` ` ` `// the frequency of numbers ` ` ` `unordered_map<` `int` `, ` `int` `> freq; ` ` ` ` ` `// Loop to iterate over the ` ` ` `// elements of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `freq[a[i]]++; ` ` ` `} ` ` ` ` ` `// Creating a DP array to keep ` ` ` `// count of max score at ith element ` ` ` `// and it will be filled ` ` ` `// in the bottom Up manner ` ` ` `vector<` `int` `> dp(*max_element(a.begin(), ` ` ` `a.end()) ` ` ` `+ 1, ` ` ` `0); ` ` ` `dp[0] = 0; ` ` ` `dp[1] = freq[1]; ` ` ` ` ` `// Loop to choose the elements of the ` ` ` `// array to delete from the array ` ` ` `for` `(` `int` `i = 2; i < dp.size(); i++) ` ` ` `dp[i] = max( ` ` ` `dp[i - 1], ` ` ` `dp[i - 2] + freq[i] * i); ` ` ` ` ` `return` `dp[dp.size() - 1]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n; ` ` ` `n = 3; ` ` ` `vector<` `int` `> a{ 1, 2, 3 }; ` ` ` ` ` `// Function Call ` ` ` `cout << findMaximumScore(a, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// maximum score of the deleting a ` `// element from an array ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the maximum ` `// score of the deleting an element ` `// from an array ` `static` `int` `findMaximumScore(` `int` `[]a, ` `int` `n) ` `{ ` ` ` ` ` `// Creating a map to keep ` ` ` `// the frequency of numbers ` ` ` `@SuppressWarnings` `(` `"unchecked"` `) ` ` ` `HashMap<Integer, ` ` ` `Integer> freq = ` `new` `HashMap(); ` ` ` ` ` `// Loop to iterate over the ` ` ` `// elements of the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(freq.containsKey(a[i])) ` ` ` `{ ` ` ` `freq.put(a[i], ` ` ` `freq.get(a[i]) + ` `1` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `freq.put(a[i], ` `1` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Creating a DP array to keep ` ` ` `// count of max score at ith element ` ` ` `// and it will be filled ` ` ` `// in the bottom Up manner ` ` ` `int` `[]dp = ` `new` `int` `[Arrays.stream(a).max().getAsInt() + ` `1` `]; ` ` ` `dp[` `0` `] = ` `0` `; ` ` ` `dp[` `1` `] = freq.get(` `1` `); ` ` ` ` ` `// Loop to choose the elements of the ` ` ` `// array to delete from the array ` ` ` `for` `(` `int` `i = ` `2` `; i < dp.length; i++) ` ` ` `dp[i] = Math.max(dp[i - ` `1` `], ` ` ` `dp[i - ` `2` `] + ` ` ` `freq.get(i) * i); ` ` ` ` ` `return` `dp[dp.length - ` `1` `]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n; ` ` ` `n = ` `3` `; ` ` ` `int` `[]a = { ` `1` `, ` `2` `, ` `3` `}; ` ` ` ` ` `// Function call ` ` ` `System.out.print(findMaximumScore(a, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# maximum score of the deleting a ` `# element from an array ` `from` `collections ` `import` `defaultdict ` ` ` `# Function to find the maximum ` `# score of the deleting an element ` `# from an array ` `def` `findMaximumScore(a, n): ` ` ` ` ` `# Creating a map to keep ` ` ` `# the frequency of numbers ` ` ` `freq ` `=` `defaultdict (` `int` `) ` ` ` ` ` `# Loop to iterate over the ` ` ` `# elements of the array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `freq[a[i]] ` `+` `=` `1` ` ` ` ` `# Creating a DP array to keep ` ` ` `# count of max score at ith element ` ` ` `# and it will be filled ` ` ` `# in the bottom Up manner ` ` ` `dp ` `=` `[` `0` `] ` `*` `(` `max` `(a) ` `+` `1` `) ` ` ` `dp[` `0` `] ` `=` `0` ` ` `dp[` `1` `] ` `=` `freq[` `1` `] ` ` ` ` ` `# Loop to choose the elements of the ` ` ` `# array to delete from the array ` ` ` `for` `i ` `in` `range` `(` `2` `, ` `len` `(dp)): ` ` ` `dp[i] ` `=` `max` `(dp[i ` `-` `1` `], ` ` ` `dp[i ` `-` `2` `] ` `+` ` ` `freq[i] ` `*` `i) ` ` ` ` ` `return` `dp[` `-` `1` `] ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `3` ` ` `a ` `=` `[` `1` `, ` `2` `, ` `3` `] ` ` ` ` ` `# Function Call ` ` ` `print` `(findMaximumScore(a, n)) ` ` ` `# This code is contributed by Chitranayal` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// maximum score of the deleting a ` `// element from an array ` `using` `System; ` `using` `System.Linq; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG{ ` ` ` `// Function to find the maximum ` `// score of the deleting an element ` `// from an array ` `static` `int` `findMaximumScore(` `int` `[]a, ` `int` `n) ` `{ ` ` ` ` ` `// Creating a map to keep ` ` ` `// the frequency of numbers ` ` ` `Dictionary<` `int` `, ` ` ` `int` `> freq = ` `new` `Dictionary<` `int` `, ` ` ` `int` `>(); ` ` ` ` ` `// Loop to iterate over the ` ` ` `// elements of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(freq.ContainsKey(a[i])) ` ` ` `{ ` ` ` `freq[a[i]] = freq[a[i]] + 1; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `freq.Add(a[i], 1); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Creating a DP array to keep ` ` ` `// count of max score at ith element ` ` ` `// and it will be filled ` ` ` `// in the bottom Up manner ` ` ` `int` `[]dp = ` `new` `int` `[a.Max() + 1]; ` ` ` `dp[0] = 0; ` ` ` `dp[1] = freq[1]; ` ` ` ` ` `// Loop to choose the elements of the ` ` ` `// array to delete from the array ` ` ` `for` `(` `int` `i = 2; i < dp.Length; i++) ` ` ` `dp[i] = Math.Max(dp[i - 1], ` ` ` `dp[i - 2] + ` ` ` `freq[i] * i); ` ` ` ` ` `return` `dp[dp.Length - 1]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n; ` ` ` `n = 3; ` ` ` `int` `[]a = { 1, 2, 3 }; ` ` ` ` ` `// Function call ` ` ` `Console.Write(findMaximumScore(a, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

4

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.

## Recommended Posts:

- Find last element after deleting every second element in array of n integers
- Maximum score possible after performing given operations on an Array
- Maximum possible middle element of the array after deleting exactly k elements
- Maximum score assigned to a subsequence of numerically consecutive and distinct array elements
- Change in Median of given array after deleting given elements
- Students with maximum average score of three subjects
- Find maximum points which can be obtained by deleting elements from array
- Maximum size of sub-array that satisfies the given condition
- Count number of ways to reach a given score in a game
- Count number of ways to reach a given score in a Matrix
- Find the Alpha Score of the Given Steps (Using BST)
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Maximum element in an array such that its previous and next element product is maximum
- Smallest index in the given array that satisfies the given condition
- Sum of absolute differences of pairs from the given array that satisfy the given condition
- Maximum items that can be bought from the cost Array based on given conditions
- Find subset with maximum sum under given condition
- Find the largest after deleting the given elements
- Find the smallest after deleting given elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.