Given an array A having N elements and two integers L and R where,
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
- Maximum possible score that can be obtained by constructing a Binary Tree based on given conditions
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- 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
- Smallest array that can be obtained by replacing adjacent pairs with their products
- 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
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
- Change in Median of given array after deleting given elements
- Steps required to visit M points in order on a circular ring of N points
- Ways to choose three points with distance between the most distant points <= L
- Find last element after deleting every second element in array of n integers
- 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
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.