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 () to arrived late ().
Given the arrival time of each student and a threshhold number of attendees, determine if the class is canceled.
Input Format
The first line of input contains , the number of test cases.
Each test case consists of two lines.
The first line has two space-separated integers,  and , the number of students (size of ) and the cancellation threshold.
The second line contains  space-separated integers () describing the arrival times for each student.
Note: Non-positive arrival times () indicate the student arrived early or on time; positive arrival times () indicate the student arrived  minutes late.
For example, there are  students who arrive at times . Four are there on time, and two arrive late. If there must be  for class to go on, in this case the class will continue. If there must be , 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
Output Format
For each test case, print the word YES if the class is canceled 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
2
4 3
-1 -3 4 2
4 2
0 -1 2 1
Sample Output
YES
NO
Explanation
For the first test case, . The professor wants at least  students in attendance, but only  have arrived on time ( and ) so the class is cancelled.
For the second test case, . The professor wants at least  students in attendance, and there are  who have arrived on time ( and ) so the class is not cancelled.

php

<?php

// Complete the angryProfessor function below.
function angryProfessor($k, $a) {
$cnt = 0;
foreach($a as $i) {
if($i <= 0)
$cnt++;
}
if($cnt < $k) {
return "YES";
} else {
return "NO";
}
}

$fptr = fopen(getenv("OUTPUT_PATH"), "w");

$stdin = fopen("php://stdin", "r");

fscanf($stdin, "%d\n", $t);

for ($t_itr = 0; $t_itr < $t; $t_itr++) {
fscanf($stdin, "%[^\n]", $nk_temp);
$nk = explode(' ', $nk_temp);

$n = intval($nk[0]);

$k = intval($nk[1]);

fscanf($stdin, "%[^\n]", $a_temp);

$a = array_map('intval',
preg_split('/ /', $a_temp, -1, PREG_SPLIT_NO_EMPTY));

$result = angryProfessor($k, $a);

fwrite($fptr, $result . "\n");
}

fclose($stdin);
fclose($fptr);

Comments

Popular posts from this blog

Intro to Tutorial Challenges

Mini-Max Sum