- Related Questions & Answers
- Find maximum points which can be obtained by deleting elements from array in Python
- Maximum array sum that can be obtained after exactly k changes in C++
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array in C++
- Maximum Points You Can Obtain from Cards in C++
- Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++
- Maximum trains for which stoppage can be provided in C++
- Maximum elements which can be crossed using given units of a and b in C++
- Program to find maximum additive score by deleting numbers in Python
- Find elements of an array which are divisible by N using STL in C++
- Maximum possible middle element of the array after deleting exactly k elements in C++
- Program to find maximum number of package that can be bought by buyers in C++
- Find a number that divides maximum array elements in C++
- Write a program in C++ to check if a string can be obtained by rotating another string by two places
- Maximum elements that can be made equal with k updates in C++
- Program to Find Out the Maximum Points From Removals in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

With respect of a given array A having N elements and two integers l and r where, 1≤ a_{x} ≤ 10^{5} and 1≤ l≤ r≤ N. We can select any element of the array (let’s say ax) and delete it, and also delete all elements equal to a_{x}+1, a_{x}+2 … a_{x}+R and a_{x}-1, a_{x}-2 … a_{x}-L from the array. This step will cost ax points. Our task is to maximize the total cost after deleting all the elements from the array.

2 1 2 3 2 2 1 l = 1, r = 1

8

Here, we choose 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.

2 4 2 10 5 l = 1, r = 2

18

Here, we choose 2 to delete, then 5 and then 10.

So total cost = 2*2 + 5 + 10 = 19.

Now, we will determine the count of all the elements. Assume an element X is chosen then, all elements in the range [X-l, X+r] will be deleted. At present, we choose the minimum range from l and r and determines up to which elements are to be deleted when element X is chosen. The results will be maximum of previously deleted elements and when element X is deleted. Now, we will implement dynamic programming to store the result of previously deleted elements and implement it further.

// C++ program to find maximum cost after // deleting all the elements form the array #include <bits/stdc++.h> using namespace std; // Shows function to return maximum cost obtained int maxCost(int a[], int m, int L, int R){ int mx1 = 0, k1; // Determine maximum element of the array. for (int p = 0; p < m; ++p) mx1 = max(mx1, a[p]); // Used to initialize count of all elements to zero. int count1[mx1 + 1]; memset(count1, 0, sizeof(count1)); // Compute frequency of all elements of array. for (int p = 0; p < m; p++) count1[a[p]]++; // Used to store cost of deleted elements. int res1[mx1 + 1]; res1[0] = 0; // Choosing minimum range from L and R. L = min(L, R); for (int num1 = 1; num1 <= mx1; num1++) { // Determines upto which elements are to be // deleted when element num is selected. k1 = max(num1 - L - 1, 0); // Obtain maximum when selecting element num or not. res1[num1] = max(res1[num1 - 1], num1 * count1[num1] + res1[k1]); } return res1[mx1]; } // Driver program int main(){ int a1[] = { 1, 1, 3, 3, 3, 2, 4 }, l1 = 1, r1 = 1; int a2[] = { 2, 4, 2, 10, 5 }, l2 = 1, r2 = 2; // size of array int n1 = sizeof(a1) / sizeof(a1[0]); int n2 = sizeof(a2) / sizeof(a2[0]); // function call to find maximum cost cout<<"Maximum Cost for First Example:" << maxCost(a1, n1, l1,r1)<<endl; cout<<"Maximum Cost for Second Example:" << maxCost(a2, n2, l2,r2); return 0; }

Maximum Cost for First Example:11 Maximum Cost for Second Example:19

Advertisements