- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Maximise the number of toys that can be purchased with amount K in C++

We are given with the prices of toys in the form of an array and an amount K in hand. The goal is to purchase the maximum no. of toys with that amount. Each element of the array is a price of a single toy, so no. of toys is no. of elements. We will sort the array of prices in ascending order so that maximum toys of less prices can be purchased first followed by costly toys.

## Input

toyprices[]= { 10, 20, 12, 15, 50, 30 } K=50

## Output

Maximum no. of toys that can be purchased : 3

**Explanation** − Sorting prices of toys in ascending order − { 10, 12, 15, 20, 30 , 50 }

Take first toy: K=50, count=1, leftover K =40 ( 50-10 ) Take second toy: K=40, count=2, leftover K =28 ( 40-12 ) Take third toy: K=28, count=13, leftover K =13 ( 28-15 ) Now K< price of next toy 20 so count=3

## Input

toyprices[]= { 50,40,30,20,10 } K=25

## Output

Maximum no. of toys that can be purchased : 1

**Explanation** − 25>10,20 but you can take only one of them as 10+20=30. Maximum count=1

## Approach used in the below program is as follows

The integer array toyprice[] stores the prices of toys.

Function maxToys( int price[], int N, int K ) takes the prices array, its length and amount

toycount is used to store the no. of toys that can be purchased, initially 0.

Variable spent is used to check how much money is spent from K.

Sort the array price[] in ascending order using sort(price, price + N);

Start traversing the array price[] from least price, price[0] till highest.

Keep adding the price of the toy in spent and check if <=K, if yes increase toycount. Which means this toy can be taken. Update spent=spent+price[i].

At last toycount has number of toys that can be purchased.

## Example

#include <bits/stdc++.h> using namespace std; int maxToys(int price[], int N, int K){ int toycount = 0; int spent = 0; //money spent upto K only // sort the prices so that minium prices are first sort(price, price + N); for (int i = 0; i < N; i++) { if (spent + price[i] <= K){ spent = spent + price[i]; toycount++; } else break; //as array is sorted } return toycount; } int main(){ int budget = 100; int toyprice[] = { 10, 120, 50, 11, 20, 100, 10, 90, 12, 15 }; int N = 10; cout <<"Maximum no. of toys that can be purchased : "<< maxToys(toyprice, N, budget) ; return 0; }

## Output

Maximum no. of toys that can be purchased : 6

- Related Articles
- Maximum elements that can be made equal with k updates in C++
- Maximum number of people that can be killed with strength P in C++
- Find the largest number that can be formed with the given digits in C++
- Maximum Number of Events That Can Be Attended in C++
- Maximum number of candies that can be bought in C
- Can The Soil From A Field Can Be Used To Make Toys ?
- C++ Program to find out the maximum amount of score that can be decreased from a graph
- C++ Program to find out the maximum amount of money that can be made from selling cars
- C++ Program to find out the maximum amount of profit that can be achieved from selling wheat
- Maximum array sum that can be obtained after exactly k changes in C++
- Find maximum number that can be formed using digits of a given number in C++
- Maximum number of threads that can be created within a process in C
- All combinations of strings that can be used to dial a number in C/C++?
- C++ program to find out the number of coordinate pairs that can be made
- C++ program to find out the maximum number of cells that can be illuminated