Open In App

Find if the Vacation can be taken or not

Given are the following values:

The task is to find if the desired vacation can be taken or not. If it can be obtained, then print Yes, else print No.



Examples:

Input: N = 1, S = 2, C = 3, H = 4, L = 5, T = 6
Output: No
Required number of hours of study = S * C * H = 24 hours of study that can be done if the vacation is taken = (N – L) * T = -24 Since available time after vacation < total time required, Hence vacation cannot be taken.



Input: N = 12, S = 5, C = 8, H = 3, L = 2, T = 20 
Output: Yes 
Required number of hours of study = S * C * H = 120 hours of study that can be done if the vacation is taken = (N – L) * T = 200 Since available time after vacation > total time required, Hence vacation can be taken.

Approach:

  1. Find the required number of hours of study by multiplying S, C and H.

required number of hours of study = S * C * H

  1. Now find the hours of study that can be done if the vacation is taken. These hours will be equivalent to the remaining days that we are left with multiplying by T hours(i.e., (N-L)*T).

hours of study that can be done if the vacation is taken = (N – L) * T

  1. Now check if the required hours are less than or equal to the hours of study that can be done if the vacation is taken. If true, then print Yes, else print No.

Below is the implementation of the above approach:




// C++ program to find
// if the Vacation can be taken or not
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find if the Vacation
// is possible or not
int isPossible(int N, int S, int C, int H,
            int L, int T)
{
 
    // Find the required number of hours of study
    int total_time_required = S * C * H;
 
    // find the hours of study that can be done
    // if the vacation is taken
    int available_time_after_vacation = (N - L) * T;
 
    // check if the required hours are less than
    // or equal to the hours of study
    // that can be done if the vacation is taken
    if (available_time_after_vacation
        >= total_time_required)
        return 1;
    return 0;
}
 
// Driver Code
int main()
{
    int N = 12, S = 5, C = 8,
        H = 3, L = 2, T = 20;
 
    if (isPossible(N, S, C, H, L, T))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
 
    N = 1, S = 2, C = 3,
    H = 4, L = 5, T = 6;
 
    if (isPossible(N, S, C, H, L, T))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
 
    return 0;
}




// Java program to find
// if the Vacation can be taken or not
class GFG
{
 
// Function to find if the Vacation
// is possible or not
static int isPossible(int N, int S, int C, int H,
                    int L, int T)
{
 
    // Find the required number of hours of study
    int total_time_required = S * C * H;
 
    // find the hours of study that can be done
    // if the vacation is taken
    int available_time_after_vacation = (N - L) * T;
 
    // check if the required hours are less than
    // or equal to the hours of study
    // that can be done if the vacation is taken
    if (available_time_after_vacation
        >= total_time_required)
        return 1;
    return 0;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 12, S = 5, C = 8,
        H = 3, L = 2, T = 20;
 
    if (isPossible(N, S, C, H, L, T) == 1)
        System.out.print("Yes" + "\n");
    else
        System.out.print("No" + "\n");
 
    N = 1; S = 2; C = 3;
    H = 4; L = 5; T = 6;
 
    if (isPossible(N, S, C, H, L, T)==1)
        System.out.print("Yes" + "\n");
    else
        System.out.print("No" + "\n");
}
}
 
// This code is contributed by 29AjayKumar




# Python3 program to find
# if the Vacation can be taken or not
 
# Function to find if the Vacation
# is possible or not
def isPossible(N, S, C, H, L, T):
     
    # Find the required number of hours of study
    total_time_required = S * C * H
     
    # find the hours of study that can be done
    # if the vacation is taken
    available_time_after_vacation = (N - L) * T
     
    # check if the required hours are less than
    # or equal to the hours of study
    # that can be done if the vacation is taken
    if (available_time_after_vacation >= total_time_required):
        return 1
    return 0
 
# Driver Code
N = 12
S = 5
C = 8
H = 3
L = 2
T = 20
 
if (isPossible(N, S, C, H, L, T)):
    print("Yes")
else:
    print("No")
 
N = 1
S = 2
C = 3
H = 4
L = 5
T = 6
 
if (isPossible(N, S, C, H, L, T)):
    print("Yes")
else:
    print("No")
         
# This code is contributed by SHUBHAMSINGH10




// C# program to find
// if the Vacation can be taken or not
using System;
 
class GFG
{
 
// Function to find if the Vacation
// is possible or not
static int isPossible(int N, int S, int C, int H,
                    int L, int T)
{
 
    // Find the required number of hours of study
    int total_time_required = S * C * H;
 
    // find the hours of study that can be done
    // if the vacation is taken
    int available_time_after_vacation = (N - L) * T;
 
    // check if the required hours are less than
    // or equal to the hours of study
    // that can be done if the vacation is taken
    if (available_time_after_vacation
        >= total_time_required)
        return 1;
    return 0;
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 12, S = 5, C = 8,
        H = 3, L = 2, T = 20;
 
    if (isPossible(N, S, C, H, L, T) == 1)
        Console.Write("Yes" + "\n");
    else
        Console.Write("No" + "\n");
 
    N = 1; S = 2; C = 3;
    H = 4; L = 5; T = 6;
 
    if (isPossible(N, S, C, H, L, T)==1)
        Console.Write("Yes" + "\n");
    else
        Console.Write("No" + "\n");
}
}
 
// This code is contributed by 29AjayKumar




// JS program to find
// if the Vacation can be taken or not
 
// Function to find if the Vacation
// is possible or not
function isPossible(N, S, C, H, L, T)
{
 
    // Find the required number of hours of study
    let total_time_required = S * C * H;
 
    // find the hours of study that can be done
    // if the vacation is taken
    let available_time_after_vacation = (N - L) * T;
 
    // check if the required hours are less than
    // or equal to the hours of study
    // that can be done if the vacation is taken
    if (available_time_after_vacation
        >= total_time_required)
        return 1;
    return 0;
}
 
// Driver Code
let N = 12, S = 5, C = 8, H = 3, L = 2, T = 20;
 
if (isPossible(N, S, C, H, L, T))
    console.log("Yes");
else
    console.log("No");
 
N = 1, S = 2, C = 3, H = 4, L = 5, T = 6;
 
if (isPossible(N, S, C, H, L, T))
    console.log("Yes");
else
    console.log("No");
     
// This code is contributed by phasing17

Output
Yes
No

Time complexity: O(1) as constant operations are being performed
Auxiliary Space: O(1)


Article Tags :