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:

  1. If some i-th object is bought from the array, the remaining amount becomes P – arr[i].
  2. 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.