Dsa
Greedy
Warm-up
Desert Scavenger
You are stranded in a desert and need to drive D kilometers to reach the nearest town. Your car is currently out of fuel. You have scavenged several fuel cans, each containing enough fuel to travel a specific number of kilometers. You want to minimize the number of cans you open to reach your destination.
Input Format:
Line 1: An integer D representing the distance to the town.
Line 2: A series of space-separated integers representing the distance range provided by each fuel can.
Output Format:
Print the minimum number of fuel cans required. If it is impossible to reach the town with all available cans, print -1.
Example:
Input:
100
20 50 10 30
Output:
3
(Explanation: Using the 50, 30, and 20 cans gives exactly 100km.)
Key concepts
sortinggreedy
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