Find if the Vacation can be taken or not
Given are the following values:
- N which are the number of days left for the exams.
- S which are the number of subjects to be prepared for the exam.
- C which are the number of chapters to be prepared for each subject.
- H which are the number of hours to prepare a chapter.
- L which are the number of days of desired vacation. During this vacation, study cannot be done.
- T which are the number of hours that one can study in a day.
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.
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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:
- Find the required number of hours of study by multiplying S, C and H.
required number of hours of study = S * C * H
- 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
- 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++
// C++ program to find// if the Vacation can be taken or not#include <bits/stdc++.h>usingnamespacestd;// Function to find if the Vacation// is possible or notintisPossible(intN,intS,intC,intH,intL,intT){// Find the required number of hours of studyinttotal_time_required = S * C * H;// find the hours of study that can be done// if the vacation is takenintavailable_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 takenif(available_time_after_vacation>= total_time_required)return1;return0;}// Driver Codeintmain(){intN = 12, S = 5, C = 8,H = 3, L = 2, T = 20;if(isPossible(N, S, C, H, L, T))cout <<"Yes"<< endl;elsecout <<"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;elsecout <<"No"<< endl;return0;}Java
// Java program to find// if the Vacation can be taken or notclassGFG{// Function to find if the Vacation// is possible or notstaticintisPossible(intN,intS,intC,intH,intL,intT){// Find the required number of hours of studyinttotal_time_required = S * C * H;// find the hours of study that can be done// if the vacation is takenintavailable_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 takenif(available_time_after_vacation>= total_time_required)return1;return0;}// Driver Codepublicstaticvoidmain(String[] args){intN =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");elseSystem.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");elseSystem.out.print("No"+"\n");}}// This code is contributed by 29AjayKumarPython3
# Python3 program to find# if the Vacation can be taken or not# Function to find if the Vacation# is possible or notdefisPossible(N, S, C, H, L, T):# Find the required number of hours of studytotal_time_required=S*C*H# find the hours of study that can be done# if the vacation is takenavailable_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 takenif(available_time_after_vacation >=total_time_required):return1return0# Driver CodeN=12S=5C=8H=3L=2T=20if(isPossible(N, S, C, H, L, T)):print("Yes")else:print("No")N=1S=2C=3H=4L=5T=6if(isPossible(N, S, C, H, L, T)):print("Yes")else:print("No")# This code is contributed by SHUBHAMSINGH10C#
// C# program to find// if the Vacation can be taken or notusingSystem;classGFG{// Function to find if the Vacation// is possible or notstaticintisPossible(intN,intS,intC,intH,intL,intT){// Find the required number of hours of studyinttotal_time_required = S * C * H;// find the hours of study that can be done// if the vacation is takenintavailable_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 takenif(available_time_after_vacation>= total_time_required)return1;return0;}// Driver CodepublicstaticvoidMain(String[] args){intN = 12, S = 5, C = 8,H = 3, L = 2, T = 20;if(isPossible(N, S, C, H, L, T) == 1)Console.Write("Yes"+"\n");elseConsole.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");elseConsole.Write("No"+"\n");}}// This code is contributed by 29AjayKumarOutput:Yes No


