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 S 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)
using namespace std;
// function to find the minimum days
int minimumDays(int S, int N, int M)
{
// If we can not buy at least a week
// supply of food during the first week
// OR We can not buy a day supply of food
// on the first day then we can't survive.
if (((N * 6) < (M * 7) && S > 6) || M > N)
return -1;
else {
// If we can survive then we can
// buy ceil(A/N) times where A is
// total units of food required.
int days = (M * S) / N;
if (((M * S) % N) != 0)
days++;
return days;
}
}
// Driver code
int main()
{
int S = 10, N = 16, M = 2;
int result = minimumDays(S, N, M);
cout << result << endl;
return 0;
}
#include <stdio.h>
// function to find the minimum days
int minimumDays(int S, int N, int M)
{
// If we cannot buy enough food
if (((N * 6) < (M * 7) && S > 6) || M > N)
return -1;
else {
int days = (M * S) / N;
if ((M * S) % N != 0)
days++;
return days;
}
}
// Driver code
int main()
{
int S = 10, N = 16, M = 2;
int result = minimumDays(S, N, M);
printf("%d\n", result);
return 0;
}
import java.util.*;
class GFG {
// function to find the minimum days
static int minimumDays(int S, int N, int M) {
// If we cannot buy enough food
if (((N * 6) < (M * 7) && S > 6) || M > N)
return -1;
else {
// total food required = M * S
int days = (M * S) / N;
// if not divisible, add one extra day
if ((M * S) % N != 0)
days++;
return days;
}
}
// Driver code
public static void main(String[] args) {
int S = 10, N = 16, M = 2;
System.out.println(minimumDays(S, N, M));
}
}
def minimumDays(S, N, M):
# If we cannot buy enough food
if ((N * 6 < M * 7 and S > 6) or M > N):
return -1
else:
days = (M * S) // N
if (M * S) % N!= 0:
days += 1
return days
if __name__ == "__main__":
S = 10
N = 16
M = 2
print(minimumDays(S, N, M))
using System;
class GFG {
// function to find the minimum days
static int MinimumDays(int S, int N, int M) {
// If we cannot buy enough food
if (((N * 6) < (M * 7) && S > 6) || M > N)
return -1;
else {
// total food required = M * S
int days = (M * S) / N;
// if not divisible, add one extra day
if ((M * S) % N != 0)
days++;
return days;
}
}
// Driver code
public static void Main() {
int S = 10, N = 16, M = 2;
Console.WriteLine(MinimumDays(S, N, M));
}
}
function minimumDays(S, N, M) {
// If we cannot buy enough food
if ((N * 6 < M * 7 && S > 6) || M > N)
return -1;
else {
let days = Math.floor((M * S) / N);
if ((M * S) % N !== 0)
days++;
return days;
}
}
// Driver code
let S = 10, N = 16, M = 2;
console.log(minimumDays(S, N, M));
Output
2