
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Maximum Points by Deleting Elements from Array in C++
Concept
With respect of a given array A having N elements and two integers l and r where, 1≤ ax ≤ 105 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 ax+1, ax+2 … ax+R and ax-1, ax-2 … ax-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.
Input
2 1 2 3 2 2 1 l = 1, r = 1
Output
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.
Input
2 4 2 10 5 l = 1, r = 2
Output
18
Here, we choose 2 to delete, then 5 and then 10.
So total cost = 2*2 + 5 + 10 = 19.
Method
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.
Example
// 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; }
Output
Maximum Cost for First Example:11 Maximum Cost for Second Example:19