
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find maximum points which can be obtained 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
- Related Articles
- 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++
- Name the clean fuel which can be obtained from cow-dung.
- maximum length of continuous silk thread that can be obtained from a cocoon."\n
- Program to find maximum additive score by deleting numbers in Python
- Maximum possible middle element of the array after deleting exactly k elements in C++
- Maximum Points You Can Obtain from Cards in C++
- What Is The Maximum Length Of Continuous Silk Thread That Can Be Obtained From A Cocoon ?
- Maximum elements which can be crossed using given units of a and b in C++
- Program to find maximum product of two distinct elements from an array in Python
- Find Two Array Elements Having Maximum Product in Java?
- Find Two Array Elements Having Maximum Sum in Java?
- 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++
