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))
- Find the smallest after deleting given elements
- Find the largest after deleting the given elements
- Find the k smallest numbers after deleting given elements
- Find the k largest numbers after deleting the given elements
- Find all numbers that divide maximum array elements
- Find a number that divides 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
- Collect maximum points in an array with k moves
- Find last element after deleting every second element in array of n integers
- Number of Co-prime pairs obtained from the sum of digits of elements in the given range
- Maximum value K such that array has at-least K elements that are >= K
- Maximum in array which is at-least twice of other elements
- Find elements larger than half of the elements in an array
- Covering maximum array elements with given value
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.