- Related Questions & Answers
- Maximum array sum that can be obtained after exactly k changes 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++
- Find elements of an array which are divisible by N using STL in C++
- Maximum elements that can be made equal with k updates in C++
- Find all elements in array which have at-least two greater elements in C++
- Find the Largest Cube formed by Deleting minimum Digits from a number in C++
- Maximum mirrors which can transfer light from bottom to right in C++
- Maximum number of segments that can contain the given points in C++
- Maximum subarray sum by flipping signs of at most K array elements in C++
- Find elements which are present in first array and not in second in C++
- Maximum value of an integer for which factorial can be calculated on a machine in C++
- C++ Program to Find Closest Pair of Points in an Array
- Find maximum height pyramid from the given array of objects in C++
- Maximum Perimeter Triangle from array in C++

- 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