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))
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.
- Reduce the array by deleting elements which are greater than all elements to its left
- Maximum array sum that can be obtained after exactly k changes
- Maximum possible middle element of the array after deleting exactly k elements
- Check if sum Y can be obtained from the Array by the given operations
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
- Maximum score of deleting an element from an Array based on given condition
- Find the largest after deleting the given elements
- Find the smallest after deleting given elements
- Find the k largest numbers after deleting the given elements
- Find the k smallest numbers after deleting given elements
- Change in Median of given array after deleting given elements
- Maximum value obtained by performing given operations in an Array
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Minimum sum obtained from groups of four elements from the given array
- Ways to choose three points with distance between the most distant points <= L
- Steps required to visit M points in order on a circular ring of N points
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.