Given an array consisting of the cost of toys. Given an integer k depicting the amount of money available to purchase toys. Write a program to find the maximum number of toys one can buy with the amount k.
Note: One can buy only 1 quantity of a particular toy.
Examples:
Input: k = 50, arr= [1, 12, 5, 111, 200, 1000, 10, 9, 12, 15 ]
Output: 6
Explanation: Toys with amount 1, 5, 9, 10, 12, and 12 can be purchased resulting in a total amount of 49. Hence, maximum number of toys is 6.
Input: k = 50, arr = [1, 12, 5, 111, 200, 1000, 10]
Output: 4
Table of Content
[Naive Approach] Subset Generation using Bitmasking - O(2ⁿ × n) Time and O(1) Space
Generate all possible subsets using bitmasking, calculate sum and count for each, and track maximum count with sum ≤ k.
- Initialize maxToys = 0
- For mask from 0 to 2ⁿ - 1
- For each bit i, if set, add arr[i] to sum and increment count
- If sum ≤ k, update maxToys
- Return maxToys
#include <iostream>
#include <vector>
using namespace std;
int toyCount(vector<int>& arr, int k) {
int n = arr.size();
int maxToys = 0;
// Generate every subset using bitmasking
for (int mask = 0; mask < (1 << n); mask++) {
int sum = 0;
int count = 0;
for (int i = 0; i < n; i++) {
// If ith toy is included in the current subset
if (mask & (1 << i)) {
sum += arr[i];
count++;
}
}
// If total cost is within budget,
// update maximum toys bought
if (sum <= k) {
maxToys = max(maxToys, count);
}
}
return maxToys;
}
int main() {
vector<int> arr = {1, 12, 5, 111, 200, 1000, 10};
int k = 50;
cout << toyCount(arr, k) << endl;
return 0;
}
// Java program to find maximum toys within budget using subset generation
import java.util.*;
class GfG {
static int toyCount(int[] arr, int k) {
int n = arr.length;
int maxToys = 0;
// Generate every subset using bitmasking
for (int mask = 0; mask < (1 << n); mask++) {
int sum = 0;
int count = 0;
for (int i = 0; i < n; i++) {
// If ith toy is included in the current subset
if ((mask & (1 << i)) != 0) {
sum += arr[i];
count++;
}
}
// If total cost is within budget, update maximum toys bought
if (sum <= k) {
maxToys = Math.max(maxToys, count);
}
}
return maxToys;
}
public static void main(String[] args) {
int[] arr = {1, 12, 5, 111, 200, 1000, 10};
int k = 50;
System.out.println(toyCount(arr, k));
}
}
# Python program to find maximum toys within budget using subset generation
def toyCount(arr, k):
n = len(arr)
maxToys = 0
# Generate every subset using bitmasking
for mask in range(1 << n):
total = 0
count = 0
for i in range(n):
# If ith toy is included in the current subset
if mask & (1 << i):
total += arr[i]
count += 1
# If total cost is within budget, update maximum toys bought
if total <= k:
maxToys = max(maxToys, count)
return maxToys
# Driver code
if __name__ == "__main__":
arr = [1, 12, 5, 111, 200, 1000, 10]
k = 50
print(toyCount(arr, k))
// C# program to find maximum toys within budget using subset generation
using System;
class GfG {
static int toyCount(int[] arr, int k) {
int n = arr.Length;
int maxToys = 0;
// Generate every subset using bitmasking
for (int mask = 0; mask < (1 << n); mask++) {
int sum = 0;
int count = 0;
for (int i = 0; i < n; i++) {
// If ith toy is included in the current subset
if ((mask & (1 << i)) != 0) {
sum += arr[i];
count++;
}
}
// If total cost is within budget, update maximum toys bought
if (sum <= k) {
maxToys = Math.Max(maxToys, count);
}
}
return maxToys;
}
static void Main(string[] args) {
int[] arr = {1, 12, 5, 111, 200, 1000, 10};
int k = 50;
Console.WriteLine(toyCount(arr, k));
}
}
// JavaScript program to find maximum toys within budget using subset generation
function toyCount(arr, k) {
const n = arr.length;
let maxToys = 0;
// Generate every subset using bitmasking
for (let mask = 0; mask < (1 << n); mask++) {
let sum = 0;
let count = 0;
for (let i = 0; i < n; i++) {
// If ith toy is included in the current subset
if (mask & (1 << i)) {
sum += arr[i];
count++;
}
}
// If total cost is within budget, update maximum toys bought
if (sum <= k) {
maxToys = Math.max(maxToys, count);
}
}
return maxToys;
}
// Driver code
const arr = [1, 12, 5, 111, 200, 1000, 10];
const k = 50;
console.log(toyCount(arr, k));
Output
4
[Expected Approach] Greedy Sorting - O(n log n) Time and O(1) Space
The idea is to buy the cheapest toys first. Sort costs in ascending order and keep adding until budget exceeds.
- Sort the array in ascending order
- Initialize sum = 0, count = 0
- For each cost in sorted array, If sum + arr[i] ≤ k, add to sum and increment count. Else break
#include <bits/stdc++.h>
using namespace std;
// Greedy Approach: Buy the cheapest toys first
int toyCount(vector<int>& arr, int k) {
int count = 0;
int sum = 0;
// Sort toy costs in increasing order
sort(arr.begin(), arr.end());
for (int i = 0; i < arr.size(); i++) {
// Check if current toy can be bought
if (sum + arr[i] <= k) {
sum += arr[i];
// Increment number of toys bought
count++;
}
}
return count;
}
int main() {
int k = 50;
vector<int> arr = {1, 12, 5, 111, 200, 1000, 10, 9, 12, 15};
cout << toyCount(arr, k) << endl;
return 0;
}
// Java program to find maximum toys within budget using greedy approach
import java.util.*;
class GfG {
// Greedy Approach: Buy the cheapest toys first
static int toyCount(int[] arr, int k) {
int count = 0;
int sum = 0;
// Sort toy costs in increasing order
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
// Check if current toy can be bought
if (sum + arr[i] <= k) {
sum += arr[i];
// Increment number of toys bought
count++;
}
}
return count;
}
public static void main(String[] args) {
int k = 50;
int[] arr = {1, 12, 5, 111, 200, 1000, 10, 9, 12, 15};
System.out.println(toyCount(arr, k));
}
}
# Python program to find maximum toys within budget using greedy approach
def toyCount(arr, k):
count = 0
total = 0
# Sort toy costs in increasing order
arr.sort()
for price in arr:
# Check if current toy can be bought
if total + price <= k:
total += price
count += 1
return count
# Driver code
if __name__ == "__main__":
k = 50
arr = [1, 12, 5, 111, 200, 1000, 10, 9, 12, 15]
print(toyCount(arr, k))
// C# program to find maximum toys within budget using greedy approach
using System;
class GfG {
// Greedy Approach: Buy the cheapest toys first
static int toyCount(int[] arr, int k) {
int count = 0;
int sum = 0;
// Sort toy costs in increasing order
Array.Sort(arr);
for (int i = 0; i < arr.Length; i++) {
// Check if current toy can be bought
if (sum + arr[i] <= k) {
sum += arr[i];
// Increment number of toys bought
count++;
}
}
return count;
}
static void Main(string[] args) {
int k = 50;
int[] arr = {1, 12, 5, 111, 200, 1000, 10, 9, 12, 15};
Console.WriteLine(toyCount(arr, k));
}
}
// JavaScript program to find maximum toys within budget using greedy approach
function toyCount(arr, k) {
let count = 0;
let sum = 0;
// Sort toy costs in increasing order
arr.sort((a, b) => a - b);
for (let i = 0; i < arr.length; i++) {
// Check if current toy can be bought
if (sum + arr[i] <= k) {
sum += arr[i];
count++;
}
}
return count;
}
// Driver code
const k = 50;
const arr = [1, 12, 5, 111, 200, 1000, 10, 9, 12, 15];
console.log(toyCount(arr, k));
Output
6