Open In App

Check if it is possible to survive on Island

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 
Examples: 

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.

Approach: 
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 than or equal to total food we require to survive in a week (7*M) then we can survive. 

Note : We are buying the food in the first 6 days because we are counting from Monday and the shop will remain close on Sunday.
If any of the above conditions are not true then we can’t survive else 

the equation can be derived as :

the amount of food that we buy should >= the amount of food required to survive.—-> equation 1

the amount of food that we buy =  number of times we buy (days) * amount of food that we get for one time buy (N)

the amount of food required to survive = the number of days we need to survive(S) * amount of food required on each day(M).

now from our equation 1:

days * N >= S * M

hence, days = ceil (S * M) / N

CPP




// C++ program to find the minimum days on which
// you need to buy food from the shop so that you
// can survive the next S days
#include <bits/stdc++.h>
using namespace std;
  
// function to find the minimum days
void survival(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)
        cout << "No\n";
    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++;
        cout << "Yes " << days << endl;
    }
}
  
// Driver code
int main()
{
    int S = 10, N = 16, M = 2;
    survival(S, N, M);
    return 0;
}


C




// C program to find the minimum days on which
// you need to buy food from the shop so that you
// can survive the next S days
#include <stdio.h>
  
// function to find the minimum days
void survival(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)
        printf("No\n");
    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++;
        printf("Yes %d\n",days);
    }
}
  
// Driver code
int main()
{
    int S = 10, N = 16, M = 2;
    survival(S, N, M);
    return 0;
}
  
// This code is contributed by rexomkar


Java




// Java program to find the minimum days on which
// you need to buy food from the shop so that you
// can survive the next S days
import java.io.*;
  
class GFG {
  
    // function to find the minimum days
    static void survival(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)
            System.out.println("No");
  
        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++;
  
            System.out.println("Yes " + days);
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int S = 10, N = 16, M = 2;
  
        survival(S, N, M);
    }
}
  
// This code is contributed by vt_m.


Python3




# Python3 program to find the minimum days on  
# which you need to buy food from the shop so 
# that you can survive the next S days
def survival(S, N, 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) and S > 6) or M > N): 
        print("No")
    else:
          
    # If we can survive then we can
    # buy ceil(A / N) times where A is
    # total units of food required.
        days = (M * S) / N
          
        if (((M * S) % N) != 0):
            days += 1
        print("Yes "),
        print(days)
  
# Driver code
S = 10; N = 16; M = 2
survival(S, N, M)
  
# This code is contributed by upendra bartwal


C#




// C# program to find the minimum days
// on which you need to buy food from 
// the shop so that you can survive 
// the next S days
using System;
  
class GFG {
  
    // function to find the minimum days
    static void survival(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)
            Console.Write("No");
        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++;
                  
            Console.WriteLine("Yes " + days);
        }
    }
  
    // Driver code
    public static void Main()
    {
        int S = 10, N = 16, M = 2;
          
        survival(S, N, M);
    }
}
  
// This code is contributed by
// Smitha Dinesh Semwal


PHP




<?php
// PHP program to find the
// minimum days on which
// you need to buy food 
// from the shop so that you
// can survive the next S days
  
// Function to find 
// the minimum $days
function survival($S, $N, $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)
        echo "No";
    else 
    {
          
        // If we can survive then we can
        // buy ceil(A/N) times where A is
        // total units of food required.
        $days = ($M * $S) / $N;
        if ((($M * $S) % $N) != 0)
            $days++;
        echo "Yes " , floor($days) ;
    }
}
  
    // Driver code
    $S = 10; $N = 16; $M = 2;
    survival($S, $N, $M);
      
// This code is contributed by anuj_67
  
?>


Javascript




<script>
// JavaScript program to find the minimum days on which
// you need to buy food from the shop so that you
// can survive the next S days
  
    // function to find the minimum days
    function survival(S, N, 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)
            document.write("No");
   
        else {
   
            // If we can survive then we can
            // buy ceil(A/N) times where A is
            // total units of food required.
            let days = (M * S) / N;
   
            if (((M * S) % N) != 0)
                days++;
   
            document.write("Yes " + Math.round(days));
        }
    }
  
// Driver Code
        let S = 10, N = 16, M = 2;
        survival(S, N, M);
          
        // This code is contributed by splevel62.
</script>


Output: 

Yes 2

Time Complexity: O(1) 
Space Complexity: O(1) 

Another Approach:

Check whether the food required for S days can be bought or not by excluding the Sundays by taking all other days to buy. If can be bought then greedily buy from the initial day until food acquired is greater than or equal to food required for S days.

C++




// C++ program to find the minimum days on which
// you need to buy food from the shop so that you
// can survive the next S days
#include <bits/stdc++.h>
using namespace std;
  
// function to find the minimum days
int minimumDays(int S, int N, int M)
{
  
    // Food required to survive S days
    double req = S * M;
  
    // If buying all possible days except sundays, but can't
    // provide the sufficient food. If total can't provide
    // then each week also can't provide.
    if ((S - S / 7) * N < req) {
        return -1;
    } // If possible get the number of days.
    else {
        return ceil(req / N);
    }
  
    // Or Simply one line code:
    // return ((S-S/7)*N<S*M) ? -1 : ceil(static_cast<double>(S*M)/N);
}
  
// Driver code
int main()
{
    int S = 10, N = 16, M = 2;
  
    int days = minimumDays(S, N, M);
    if (days != -1) {
        cout << "Yes " << days << endl;
    }
    else {
        cout << "No" << endl;
    }
  
    return 0;
}


Java




// Java program to find the minimum days on which
// you need to buy food from the shop so that you
// can survive the next S days
  
import java.util.*;
  
class GFG {
  
    // function to find the minimum days
    static int minimumDays(int S, int N, int M)
    {
  
        // Food required to survive S days
        double req = S * M;
  
        // If buying all possible days except sundays, but
        // can't provide the sufficient food. If total can't
        // provide then each week also can't provide.
        if ((S - S / 7) * N < req) {
            return -1;
        } // If possible get the number of days.
        else {
            return (int)Math.ceil(req / N);
        }
  
        // Or Simply one line code:
        // return ((S-S/7)*N<S*M) ? -1 :
        // ceil(static_cast<double>(S*M)/N);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int S = 10, N = 16, M = 2;
  
        int days = minimumDays(S, N, M);
        if (days != -1) {
            System.out.println("Yes " + days);
        }
        else {
            System.out.println("No");
        }
    }
}
  
// This code is contributed by phasing17


Python3




# Python3 program to find the minimum days on which
# you need to buy food from the shop so that you
# can survive the next S days
from math import ceil
  
# function to find the minimum days
def minimumDays(S, N, M):
  
    # Food required to survive S days
    req = S * M;
  
    # If buying all possible days except sundays, but can't
    # provide the sufficient food. If total can't provide
    # then each week also can't provide.
    if ((S - S // 7) * N < req) :
        return -1;
     # If possible get the number of days.
    else :
        return ceil(req / N);
      
# Driver code
S = 10
N = 16
M = 2;
  
days = minimumDays(S, N, M);
if (days != -1) :
    print("Yes", days);
  
else :
    print("No");
  
# This code is contributed by phasing17


C#




// C# program to find the minimum days on which
// you need to buy food from the shop so that you
// can survive the next S days
using System;
using System.Collections.Generic;
  
class GFG
{
  
  // function to find the minimum days
  static int minimumDays(int S, int N, int M)
  {
  
    // Food required to survive S days
    double req = S * M;
  
    // If buying all possible days except sundays, but
    // can't provide the sufficient food. If total can't
    // provide then each week also can't provide.
    if ((S - S / 7) * N < req) {
      return -1;
    } // If possible get the number of days.
    else {
      return (int)Math.Ceiling(req / N);
    }
  
    // Or Simply one line code:
    // return ((S-S/7)*N<S*M) ? -1 :
    // ceil(static_cast<double>(S*M)/N);
  }
  
  // Driver code
  public static void Main(string[] args)
  {
    int S = 10, N = 16, M = 2;
  
    int days = minimumDays(S, N, M);
    if (days != -1) {
      Console.WriteLine("Yes " + days);
    }
    else {
      Console.WriteLine("No");
    }
  }
}
  
// This code is contributed by phasing17


Javascript




// JS program to find the minimum days on which
// you need to buy food from the shop so that you
// can survive the next S days
  
  
// function to find the minimum days
function minimumDays(S, N, M)
{
  
    // Food required to survive S days
    let req = S * M;
  
    // If buying all possible days except sundays, but can't
    // provide the sufficient food. If total can't provide
    // then each week also can't provide.
    if ((S - S / 7) * N < req) {
        return -1;
    } // If possible get the number of days.
    else {
        return Math.ceil(req / N);
    }
  
    // Or Simply one line code:
    // return ((S-S/7)*N<S*M) ? -1 : ceil(static_cast<double>(S*M)/N);
}
  
// Driver code
let S = 10, N = 16, M = 2;
  
let days = minimumDays(S, N, M);
if (days != -1) {
    console.log("Yes", days);
}
else {
    console.log("No");
}
  
  
// This code is contributed by phasing17


Output:

Yes 2

Time Complexity: O(1) 
Space Complexity: O(1) 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads