VOOZH about

URL: https://www.geeksforgeeks.org/dsa/survival/

⇱ Check if it is possible to survive on Island - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if it is possible to survive on Island

Last Updated : 25 Apr, 2026


Geekina got stuck on an island. There is only one shop on this island and it is open on all days of the week except for Sunday. Consider following constraints:Examples: 

  • N β€“ The 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 she needs to survive for the next S days, initially she has no food.
Find the minimum number of days on which you need to buy food from the shop so that she can survive the next days. If it is not possible to survive for S days then return -1.

Input : S = 10, N = 16, M = 2 
Output : 2 
Explanation : 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 : S = 10, N = 20, M = 30 
Output : -1
Explanation : 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.

Since food can only be bought for 6 days a week (shop closed on Sunday), we must ensure that the total food we can buy in a week is sufficient for 7 days of consumption.

Survival is not possible in the following cases.

  • Food bought daily is less than food needed, i.e., M > N
  • Number of survival days include Sundays and food bought in 6 days is less than needed for 7 days, i.e.,S > 6 and N * 6 < M * 7

If survival is possible, then compute the total food needed for S days and divide it by the maximum food we can buy per day to get the minimum buying days, i.e., days = ceil((M Γ— S) / N)


Output
2
Comment