Maximum items that can be bought from the cost Array based on given conditions
Last Updated :
18 Feb, 2022
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 less cost. This type of algorithm is known as a 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++
#include <bits/stdc++.h>
using namespace std;
int number( int a[], int n, int p, int k)
{
sort(a, a + n);
int pre[n] = { 0 }, val, i,
j, ans = 0;
pre[0] = a[0];
if (pre[0] <= p)
ans = 1;
for (i = 1; i < k - 1; i++) {
pre[i] = pre[i - 1] + a[i];
if (pre[i] <= p)
ans = i + 1;
}
pre[k - 1] = a[k - 1];
for (i = k - 1; i < n; i++) {
if (i >= k) {
pre[i] += pre[i - k] + a[i];
}
if (pre[i] <= p)
ans = i + 1;
}
return ans;
}
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;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int number( int [] a, int n,
int p, int k)
{
Arrays.sort(a);
int [] pre = new int [n];
int val, i, j, ans = 0 ;
pre[ 0 ] = a[ 0 ];
if (pre[ 0 ] <= p)
ans = 1 ;
for (i = 1 ; i < k - 1 ; i++)
{
pre[i] = pre[i - 1 ] + a[i];
if (pre[i] <= p)
ans = i + 1 ;
}
pre[k - 1 ] = a[k - 1 ];
for (i = k - 1 ; i < n; i++)
{
if (i >= k)
{
pre[i] += pre[i - k] + a[i];
}
if (pre[i] <= p)
ans = i + 1 ;
}
return ans;
}
public static void main(String[] args)
{
int n = 5 ;
int [] arr = { 2 , 4 , 3 , 5 , 7 };
int p = 11 ;
int k = 2 ;
System.out.println(number(arr, n, p, k));
}
}
|
Python3
def number(a, n, p, k):
a.sort()
pre = [ ]
for i in range (n):
pre.append( 0 )
ans = 0
val = 0
i = 0
j = 0
pre[ 0 ] = a[ 0 ]
if pre[ 0 ] < = p:
ans = 1
for i in range ( 1 , k - 1 ):
pre[i] = pre[i - 1 ] + a[i]
if pre[i] < = p:
ans = i + 1
pre[k - 1 ] = a[k - 1 ]
for i in range (k - 1 , n):
if i > = k:
pre[i] + = pre[i - k] + a[i]
if pre[i] < = p:
ans = i + 1
return ans
n = 5
arr = [ 2 , 4 , 3 , 5 , 7 ]
p = 11
k = 2
print (number(arr, n, p, k))
|
C#
using System;
using System.Collections;
class GFG{
static int number( int [] a, int n,
int p, int k)
{
Array.Sort(a);
int [] pre = new int [n];
int i, ans = 0;
pre[0] = a[0];
if (pre[0] <= p)
ans = 1;
for (i = 1; i < k - 1; i++)
{
pre[i] = pre[i - 1] + a[i];
if (pre[i] <= p)
ans = i + 1;
}
pre[k - 1] = a[k - 1];
for (i = k - 1; i < n; i++)
{
if (i >= k)
{
pre[i] += pre[i - k] + a[i];
}
if (pre[i] <= p)
ans = i + 1;
}
return ans;
}
static public void Main ()
{
int n = 5;
int [] arr = { 2, 4, 3, 5, 7 };
int p = 11;
int k = 2;
Console.WriteLine(number(arr, n, p, k));
}
}
|
Javascript
<script>
function number(a, n, p, k)
{
a.sort();
var pre = Array(n).fill(0), val, i,
j, ans = 0;
pre[0] = a[0];
if (pre[0] <= p)
ans = 1;
for (i = 1; i < k - 1; i++) {
pre[i] = pre[i - 1] + a[i];
if (pre[i] <= p)
ans = i + 1;
}
pre[k - 1] = a[k - 1];
for (i = k - 1; i < n; i++) {
if (i >= k) {
pre[i] += pre[i - k] + a[i];
}
if (pre[i] <= p)
ans = i + 1;
}
return ans;
}
var n = 5;
var arr = [2, 4, 3, 5, 7];
var p = 11;
var k = 2;
document.write( number(arr, n, p, k));
</script>
|
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...