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))
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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
- Maximum value obtained by performing given operations in an Array
- Find the k smallest numbers after deleting given elements
- Find the k largest numbers after deleting the given elements
- Minimum sum obtained from groups of four elements from the given array
- Find maximum xor of k elements in an array
- Find the minimum and maximum sum of N-1 elements of the 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
- Find maximum array sum after making all elements same with repeated subtraction
- Find integers that divides maximum number of elements of the array
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.