No Time To Wait (NOTIME) Solution — Codechef MarchLong Challenge
Problem Statement
Only x hours are left for the March Long Challenge and Chef is only left with the last problem unsolved. However, he is sure that he cannot solve the problem in the remaining time. From experience, he figures out that he needs exactly H hours to solve the problem.
Now Chef finally decides to use his special power which he has gained through years of intense yoga. He can travel back in time when he concentrates. Specifically, his power allows him to travel to N different time zones, which are T1,T2,…,TN hours respectively behind his current time.
Find out whether Chef can use one of the available time zones to solve the problem and submit it before the contest ends.
Input
- The first line of the input contains three space-separated integers N, H and x.
- The second line contains N space-separated integers T1,T2,…,TN.
Output
Print a single line containing the string "YES"
if Chef can solve the problem on time or "NO"
if he cannot.
You may print each character of each string in uppercase or lowercase (for example, the strings “yEs”, “yes”, “Yes” and “YES” will all be treated as identical).
Constraints
- 1≤N≤100
- 1≤x<H≤100
- 1≤Ti≤100 for each valid i
Subtasks
Subtask #1 (100 points): original constraints
Example Input 1
2 5 3
1 2
Example Output 1
YES
Explanation
Chef already has 3 hours left. He can go to the 2-nd time zone, which is 2 hours back in time. Then he has a total of 3+2=5 hours, which is sufficient to solve the problem.
Example Input 2
2 6 3
1 2
Example Output 2
NO
Explanation
If Chef goes to the 1-st time zone, he will have 3+1=4 hours, which is insufficient to solve the problem.
If he goes to the 2-nd time zone, he will have 3+2=5 hours, which is also insufficient to solve the problem.
Since none of the time travel options can be used to gain sufficient time to solve the problem, Chef is incapable of solving it.
Code (Solution)
The code has been implemented in Java:
/* package codechef; // don't place package name! */import java.util.*;import java.lang.*;import java.io.*;/* Name of the class has to be "Main" only if the class is public. */class Codechef{public static void main (String[] args) throws java.lang.Exception{// your code goes hereScanner sc = new Scanner(System.in);int n = sc.nextInt();int h = sc.nextInt();int x = sc.nextInt();int arr[] = new int[n];for(int i=0;i<n;i++){arr[i] = sc.nextInt();}for(int i=0;i<n;i++){if((x+arr[i])==h){System.out.println("YES");return;}}System.out.println("NO");}}
Hope you would have liked the article. Please give 50 claps to this article and follow me for more future programming related blogs.
References