Angry ProfessorA Discrete Mathematics professor has a class of students. Frustrated with their lack of discipline, he decides to cancel class if fewer than some number of students are present when class starts. Arrival times go from on time ( arrivalTime <= 0 ) to arrive late ( arrivalTime > 0 ). Given the arrival time of each student and a threshold number of attendees, determine if the class is cancelled. Input Format The first line of input contains t, the number of test cases. Each test case consists of two lines.
Note: Non-positive arrival times ( a[i] < = 0 ) indicate the student arrived early or on time; positive arrival times ( a[i] > 0 ) indicate the student arrived a[i] minutes late.For example, there are n = 6 students who arrive at times a =[ -1, -1, 0, 0, 1, 1 ]. Four are there on time, and two arrive late. If there must be k = 4 for class to go on, in this case the class will continue. If there must be k = 5, then class is cancelled. Function DescriptionComplete the angryProfessor function in the editor below. It must return YES if class is cancelled, or NO otherwise. angryProfessor has the following parameter(s):
Constraints
Output Format For each test case, print the word YES if the class is cancelled or NO if it is not. Note If a student arrives exactly on time, the student is considered to have entered before the class started.Sample Input Sample Output YES {YES says, class will be cancelled} NO {NO says, class will not be cancelled} Explanation For the first test case, k = 3. The professor wants atleast 3 students in attendance, but only 2 have arrived on time ( -3 and -1 ) so the class is cancelled. For the second test case, k = 2. The professor wants at least 2 students in attendance, and there are 2 who have arrived on time ( 0 and -1 ) so the class is not cancelled. Code of above problem is given below: Explanation of above code1. Created variables n, count, t, k, and i 2. Entered the no. of test cases 3. While( t-- ) is a loop used for executing no. of test cases t (no. of times entered by user), let value entered by user be 2, i.e. t = 2, hence loop will run for two times. 4. Enter the no. of students to be entered for a class, let n = 4 5. Enter the minimum strength required to start a class, as professor is angry and will not start his class until and unless minimum strength is there on or before start of class. Let k = 3 be the minimum no. of strength required to start a class. 6. Created an array at[n], that is an array of size same as no. of students in class. 7. for( i = 0; i < n; i++ ) 8. The above loop works to enter the values in an array, which enter the values in positive, negative and zero where negative values represent arrival before starting time of class, and positive value represent arrival after starting time of class, 0 represent on time, i.e. student entered exactly the time allotted by angry professor. 9. Let values entered by user are -1 -3 4 2, i.e. at[ -1 -3 4 2 ], arrival time of 4 students, where negative value represents arrival before starting time of class, and positive value represents arrival after starting time of class, 0 represents on time 10. Now check the if( at[i] <= 0 ) for each value of at[ -1 -3 4 2 ], since -1 < = 0 hence count++, i.e. count = 1 11. Since current value of count = 2 < k( 3 ), hence YES will get printed. YES means that as minimum strength required by professor to start class was k = 3, but count = 2 represent that only two students reached on or before time, hence YES class will be cancelled by professor. 12. Now taking test case 2 13. Enter the no. of students to be entered for a class, let 14. Enter the minimum strength required to start a class, as professor is angry and will not start his class until and unless minimum strength is there on or before start of class. Let k = 2 be the minimum no. of strength required to start a class. 15. Created an array at[n], that is an array of size same as no. of students in class. 16. for( i = 0; i < n; i++ ) 17. The above loop works to enter the values in an array, which enter the values in positive, negative and zero where negative values represent arrival before starting time of class, and positive value represents arrival after starting time of class, 0 represent on time, i.e. student entered exactly the time allotted by angry professor. 18. Let values entered by user are 0, -1, 2, 1, i.e. at[0, -1, 2, 1], arrival time of 4 students, where negative values represent arrival before starting time of class, and positive value represent arrival after starting time of class, 0 represent on time 19. Now check the if( at[i] <= 0 ) for each value of at[ 0, -1, 2, 1], since 0 < = 0 hence count++, i.e. count = 1 20. Since current value of count = 2 = k( 2 ), hence NO will get printed. NO means that as minimum strength required by professor to start class was k = 2, and count = 2 represent that only two students reached on or before time, hence NO class will be cancelled by professor.
Next TopicApple and Orange Problem Solution
|