Given an array A having N elements and two integers L and R where, and . You can choose any element of the array (let’s say ax) and delete it, and also delete all elements equal to ax+1, ax+2 … ax+R and ax-1, ax-2 … ax-L from the array. This step will cost ax points. The task is to maximize the total cost after deleting all the elements from the array.
Input : 2 1 2 3 2 2 1 L = 1, R = 1 Output : 8 We select 2 to delete, then (2-1)=1 and (2+1)=3 will need to be deleted, for given L and R range respectively. Repeat this until 2 is completely removed. So, total cost = 2*4 = 8. Input : 2 4 2 9 5 L = 1, R = 2 Output : 18 We select 2 to delete, then 5 and then 9. So total cost = 2*2 + 5 + 9 = 18.
Approach: We will find the count of all the elements. Now let’s say an element X is selected then, all elements in the range [X-L, X+R] will be deleted. Now we select the minimum range from L and R and finds up to which elements are to be deleted when element X is selected. Our results will be maximum of previously deleted elements and when element X is deleted. We will use dynamic programming to store the result of previously deleted elements and use it further.
Time Complexity: O(max(A))
- Maximum possible middle element of the array after deleting exactly k elements
- Maximum array sum that can be obtained after exactly k changes
- Find the largest after deleting the given elements
- Find the smallest after deleting given elements
- Minimum sum obtained from groups of four elements from the given array
- Find the k largest numbers after deleting the given elements
- Find the k smallest numbers after deleting given elements
- Find maximum xor of k elements in an array
- Find the maximum elements in the first and the second halves of the Array
- Find a number that divides maximum array elements
- Find all numbers that divide maximum array elements
- Recursive Programs to find Minimum and Maximum elements of array
- Find maximum array sum after making all elements same with repeated subtraction
- Find integers that divides maximum number of elements of the array
- Collect maximum points in an array with k moves
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.