You are a poor person in an island. There is only one shop in this island, this shop is open on all days of the week except for Sunday. Consider following constraints:
- N – Maximum unit of food you can buy each day.
- S – Number of days you are required to survive.
- M – Unit of food required each day to survive.
Currently, it’s Monday, and you need to survive for the next S days.
Find the minimum number of days on which you need to buy food from the shop so that you can survive the next S days, or determine that it isn’t possible to survive.
Input : S = 10 N = 16 M = 2
Output : Yes 2
Explanation 1: One possible solution is to buy a box on the first day (Monday), it’s sufficient to eat from this box up to 8th day (Monday) inclusive. Now, on the 9th day (Tuesday), you buy another box and use the chocolates in it to survive the 9th and 10th day.
Input : 10 20 30
Output : No
Explanation 2: You can’t survive even if you buy food because the maximum number of units you can buy in one day is less the required food for one day.
In this problem, the greedy approach of buying the food for some consecutive early days is the right direction.
If we can survive for the first 7 days then we can survive any number of days for that we need to check two things
-> Check whether we can survive one day or not.
-> (S >= 7) If we buy food in the first 6 days of the week and we can survive for the week i.e. total food we can buy in a week (6*N) is greater then or equal to total food we require to survive in a week (7*M) then we can survive.
If any of the above conditions are not true then we can’t survive else the minimum number of days required to buy food will be = ceil(total food required/units of food we can buy each day)
Time Complexity: O(1)
Space Complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- The Blue – eyed Island puzzle
- Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps
- Minimum LCM and GCD possible among all possible sub-arrays
- Check if it is possible to make array equal by doubling or tripling
- Check if it is possible to create a polygon with a given angle
- Check if it is possible to sort the array after rotating it
- Check if it is possible to return to the starting position after moving in the given directions
- Check if it is possible to serve customer queue with different notes
- Check whether it is possible to make both arrays equal by modifying a single element
- Check if it is possible to draw a straight line with the given direction cosines
- Check if it is possible to create a polygon with given n sides
- Check if it is possible to make two martices strictly increasing by swapping corresponding values only
- Check if its possible to make sum of the array odd with given Operations
- Check if an N-sided Polygon is possible from N given angles
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Find minimum elements after considering all possible transformations
- Find maximum sum possible equal sum of three stacks
- Find if it is possible to get a ratio from given ranges of costs and quantities
- Find all possible triangles with XOR of sides zero
- Sum of Areas of Rectangles possible for an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.