Wednesday, March 23, 2016

Angry Professor - Hacker Rank Solution

A Discrete Mathematics professor has a class of NN students. Frustrated with their lack of discipline, he decides to cancel class if fewer than KK students are present when class starts.
Given the arrival time of each student, determine if the class is canceled.
Input Format
The first line of input contains TT, the number of test cases.
Each test case consists of two lines. The first line has two space-separated integers, NN (students in the class) and KK (the cancelation threshold).
The second line contains NN space-separated integers (a1,a2,,aNa1,a2,,aN) describing the arrival times for each student.
Note: Non-positive arrival times (ai0ai0) indicate the student arrived early or on time; positive arrival times (ai>0ai>0) indicate the student arrived aiai minutes late.
Output Format
For each test case, print the word YES if the class is canceled or NO if it is not.
Constraints
  • 1T101T10
  • 1N10001N1000
  • 1KN1KN
  • 100ai100,where i[1,N]100ai100,where i[1,N]
Note
If a student arrives exactly on time (ai=0)(ai=0), 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, K=3K=3. The professor wants at least 33 students in attendance, but only 22 have arrived on time (33 and 11). Thus, the class is canceled.
For the second test case, K=2K=2. The professor wants at least 22 students in attendance, and there are 22 who have arrived on time (00 and 11). Thus, the class is not canceled.

----------------------------------------------------------------------------------------------------

  Angry Professor -  Hacker Rank Solution

 

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int t;
    scanf("%d",&t);
    for(int a0 = 0; a0 < t; a0++){
        int n;
        int k;
        int count=0;
        scanf("%d %d",&n,&k);
        int a[n];
        for(int a_i = 0; a_i < n; a_i++){
           scanf("%d",&a[a_i]);
        }
       
           for(int i=0;i<n;i++)
        {
             if(a[i]<=0) count++;
           }
          if(count>=k)
              printf("NO \n");
          else
              printf("YES \n");
    }
    return 0;
}

Angry Professor -  Hacker Rank Solution

No comments:

Post a Comment

Powered by Blogger.