Javatpoint Logo
Javatpoint Logo

Angry Professor

A 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.

  • The first line has two space-separated integers, n and k, the number of students (size of ) and the cancellation threshold.
  • The second line contains space-separated integers (a[1], a[2], . . . a[n] ) describing the arrival times for each student.

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 Description

Complete the angryProfessor function in the editor below. It must return YES if class is cancelled, or NO otherwise.

angryProfessor has the following parameter(s):

  • k: the threshold number of students
  • a: an array of integers representing arrival times

Constraints

  • 1 < = t < = 10
  • 1 < = n < = 1000
  • 1 < = k <= n
  • -100 < = a[i] < = 100, where i ? [ 1, 2, . . . n ]

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 code

1. 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++ )
scanf( " %d ", &at[i] );
if( at[i] <= 0 )
count++;

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
since -3 < = 0 hence count++, i.e. count = 2
since 4 < = 0 ( FALSE) hence count++ will not get executed.
since -2 < = 0 ( FALSE) hence count++ will not get executed.

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
n = 4

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++ )
scanf( " %d ", &at[i] );
if( at[i] <= 0 )
count++;

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
since -1 < = 0 hence count++, i.e. count = 2
since 2 < = 0 ( FALSE) hence count++ will not get executed.
since 1 < = 0 ( FALSE) hence count++ will not get executed.

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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA