Check if it is possible to survive on Island
Sujata 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:
- 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.
Find the minimum number of days on which you need to buy food from the shop so that she can survive the next S days, or determine that it isn’t possible to survive.
Example 1:
Input: S = 10, N = 16, M = 2
Output: 2
Explaination: 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.
Example 2:
Input: S = 10, N = 20, M = 30
Output: -1
Explaination: She can’t survive even if
she buy food because the maximum number
of units she can buy in 1 day is less the
required food for 1 day.
method : greedy approach
1)take a variable z and store all days except sundays ie. z=s-s/7
2)if z*n is smaller than m*s (maximum number of units she can buy in z days is less the required food for s days.) than return -1
3)else return the minimum number of days on which she needs to buy food
i) ie.return (s*m)/n if s*m is divsisble by n .
ii) return ((s*m)/n)+1 if s*m is not divsisble by n.
c++ implementation:
int minimumDays(int s, int n, int m)
{
int z= s-s/7;
if(z*n<m*s)
{
return -1;
}
else
{
if((s*m)%n==0)
{
return (s*m)/n;
}
else
{
return ((s*m)/n)+1;
}
}
}
No comments