Friday, April 8, 2016

Angry Professor - Hackerrank

https://www.hackerrank.com/challenges/angry-professor

A Discrete Mathematics professor has a class of $N$ students. Frustrated with their lack of discipline, he decides to cancel class if fewer than $K$ students are present when class starts.
Given the arrival time of each student, determine if the class is canceled.

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$ (students in the class) and $K$ (the cancelation threshold).
The second line contains $N$ space-separated integers (${a}_{1},{a}_{2},\dots ,{a}_{N}$) describing the arrival times for each student.
Note: Non-positive arrival times (${a}_{i}\le 0$) indicate the student arrived early or on time; positive arrival times (${a}_{i}>0$) indicate the student arrived ${a}_{i}$ minutes late.
Scanner sc = new Scanner(System.in);
//no of test cases
int T = Integer.parseInt(sc.nextLine());

for(int count=0;count<T;count++)
{
String[] constraints = sc.nextLine().split(" ");
//n and k
int N =Integer.parseInt(constraints[0]);
int K =Integer.parseInt(constraints[1]);
//count no of students inside the class
int noOfStuds =0;
//all the students times are stored in string array
String[] students = sc.nextLine().split(" ");

for(int i=0;i<N;i++)
{
int stud = Integer.parseInt(students[i]);
// if it is negative or zero the student arrived before the class
if(stud<=0)
noOfStuds++;
//k students are present in the class so no need to cancel the class, so NO
if(noOfStuds>=K)
{
System.out.println("NO");
break;
}
}
// if K students are not there in the class the class is cancelled so YES
if(noOfStuds<K)
System.out.println("YES");
}