# Maximum items that can be bought from the cost Array based on given conditions

Given an array **arr[]** of size **N** where every index in the array represents the cost of buying an item and two numbers **P, K**. The task is to find the maximum number of items which can be bought such that:

- If some i-th object is bought from the array, the remaining amount becomes
**P – arr[i]**. - We can buy
**K**items, not necessarily consecutive, at a time by paying only for the item whose cost is maximum among them. Now, the remaining amount would be**P – max(cost of K items)**.

**Examples:**

Input:arr[] = {2, 4, 3, 5, 7}, P = 6, K = 2

Output:3

Explanation:

We can buy the first item whose cost is 2. So, the remaining amount is P = 6 – 2 = 4.

Now, we can choose the second and third item and pay for the maximum one which is max(4, 3) = 4, and the remaining amount is 4 – 4 = 0.

Therefore, the total number of items bought is 3.

Input:arr[] = {2, 4, 3, 5, 7}, P = 11, K = 2

Output:4

Explanation:

We can buy the first and third item together and pay for only the maximum one which is max(2, 3) = 3. The remaining amount is P = 11 – 3 = 8.

Now, we can buy the second and fourth item and pay for the maximum one which is max(4, 5) = 5. The remaining amount is P = 8 – 5 = 3. Now, we cant buy any item further.

**Approach:** The idea is to use the concept of sorting and prefix sum array.

- Sort the given array arr[].
- Find the prefix sum for the array arr[].
- The idea behind sorting is that the maximum number of items can be bought only when we buy the items with the less cost. This type of algorithm is known is greedy algorithm.
- And, we use the prefix sum array to find the cost of buying the items.

Below is the implementation of the above approach:

`// C++ program to find the ` `// maximum number of items ` `// that can be bought from ` `// the given cost array ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the ` `// maximum number of items ` `// that can be bought from ` `// the given cost array ` `int` `number(` `int` `a[], ` `int` `n, ` `int` `p, ` `int` `k) ` `{ ` ` ` `// Sort the given array ` ` ` `sort(a, a + n); ` ` ` ` ` `// Variables to store the prefix ` ` ` `// sum, answer and the counter ` ` ` `// variables ` ` ` `int` `pre[n] = { 0 }, val, i, ` ` ` `j, ans = 0; ` ` ` ` ` `// Initializing the first element ` ` ` `// of the prefix array ` ` ` `pre[0] = a[0]; ` ` ` ` ` `// If we can buy at least one item ` ` ` `if` `(pre[0] <= p) ` ` ` `ans = 1; ` ` ` ` ` `// Iterating through the first ` ` ` `// K items and finding the ` ` ` `// prefix sum ` ` ` `for` `(i = 1; i < k - 1; i++) { ` ` ` `pre[i] = pre[i - 1] + a[i]; ` ` ` ` ` `// Check the number of items ` ` ` `// that can be bought ` ` ` `if` `(pre[i] <= p) ` ` ` `ans = i + 1; ` ` ` `} ` ` ` ` ` `pre[k - 1] = a[k - 1]; ` ` ` ` ` `// Finding the prefix sum for ` ` ` `// the remaining elements ` ` ` `for` `(i = k - 1; i < n; i++) { ` ` ` `if` `(i >= k) { ` ` ` `pre[i] += pre[i - k] + a[i]; ` ` ` `} ` ` ` ` ` `// Check the number of iteams ` ` ` `// that can be bought ` ` ` `if` `(pre[i] <= p) ` ` ` `ans = i + 1; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `int` `arr[] = { 2, 4, 3, 5, 7 }; ` ` ` `int` `p = 11; ` ` ` `int` `k = 2; ` ` ` ` ` `cout << number(arr, n, p, k) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Get maximum items when other items of total cost of an item are free
- Maximum length sub-array which satisfies the given conditions
- Maximum length sub-array which satisfies the given conditions
- Maximum number of candies that can be bought
- Maximum sub-array sum after dividing array into sub-arrays based on the given queries
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Maximum litres of water that can be bought with N Rupees
- Find the maximum cost of an array of pairs choosing at most K pairs
- Find the Maximum possible Sum for the given conditions
- Find maximum Subsequence Sum according to given conditions
- Buy Maximum Stocks if i stocks can be bought on i-th day
- Maximum profit by selling N items at two markets
- Generate an array B[] from the given array A[] which satisfies the given conditions
- Minimum, maximum and average price values for all the items of given type
- Generate elements of the array following given conditions
- Split the array into equal sum parts according to given conditions
- Count of triplets in an array that satisfy the given conditions
- Generate an array of size K which satisfies the given conditions
- Find an array of size N that satisfies the given conditions
- Generate an array using given conditions from a given 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 contribute@geeksforgeeks.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.