Dsa
Greedy
Medium
Maximum Different Fruit Selection
A vendor sells different types of fruits, represented by integers. Given a budget and a list of fruit prices, your goal is to maximize the different types of fruits you can buy without exceeding the budget. You need to return the maximum number of different fruits you can afford within the given budget.
Input Format:
- The first line contains an integer b (1 ≤ b ≤ 1000) - the budget.
- The second line contains an integer n (1 ≤ n ≤ 100) - the total number of fruit types.
- The third line contains n space-separated integers representing the prices of the fruits.
Output Format:
- A single integer - the maximum number of different fruit types that can be bought within the budget.
Example:
Input:
10
5
1 2 3 4 5
Output:
4
Key concepts
greedyoptimizationarrays
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now