Angry Professor Hackerrank problem solution- Coderinme

Angry Professor Hackerrank

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.

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(students in the class) and K(the cancelation threshold). The second line contains N space-separated integers (a1,a2,…,aN) describing the arrival times for each student.

Note: Non-positive arrival times (ai<=0) indicate the student arrived early or on time; positive arrival times (ai>=0) indicate the student arrived ai minutes late.

-100<=ai<=100 i{1,N}
Output Format

For each test case, print the word YES if the class is canceled or NO if it is not.

If a student arrives exactly on time (ai=0), the student is considered to have entered before the class started.

Sample Input

4 3
-1 -3 4 2
4 2
0 -1 2 1
Sample Output


For the first test case, K=3. The professor wants at least 3 students in attendance, but only 2 have arrived on time (-1 and -3). Thus, the class is canceled.

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). Thus, the class is not canceled.

Solution in C++

You only need to count the number of students whose arrival time is less than or equal to 0. If this count is less than K, then the class is cancelled. Otherwise, it is not.

using namespace std;

int main() {
    int T;
    cin >> T;
    int x;
    int N, k, sum;
    for (int i = 0; i < T; i++) {
        sum = 0;
        cin >> N >> k;
        for(int i = 0; i < N; i++) {
            cin >> x;
            if (x <= 0) {
        if (sum < k) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
    return 0;

Solution in C++ II

#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;
    for(int a0 = 0; a0 < t; a0++){
        int n; 
        int k; 
        int c=0;
        scanf("%d %d",&n,&k);
        int a[n];
        for(int a_i = 0; a_i < n; a_i++){
    return 0;

Solution in java

import java.util.StringTokenizer;

public class AngryProf {

	public static void main(String args[] ) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(;
	    PrintWriter w = new PrintWriter(System.out);
	    int t = ip(br.readLine());
	    while(t-- > 0) {
	    	StringTokenizer st1 = new StringTokenizer(br.readLine());
	    	int n = ip(st1.nextToken());
	    	int k = ip(st1.nextToken());
	        StringTokenizer st2 = new StringTokenizer(br.readLine());
	        int a[] = new int[n];
	        for(int i=0;i<n;i++)
	       	   a[i] = ip(st2.nextToken());
	        int count = 0;
	        for(int i=0;i<n;i++)
	        	if(a[i]<=0)	count++;
	        w.println(count < k ? "YES" : "NO");
	public static int ip(String s){
		return Integer.parseInt(s);

Solution in short python

for _ in range(input()):
    n, k = map(int, raw_input().split())
    a1 = filter(lambda x:x <= 0, map(int, raw_input().split()))
    print ["YES", "NO"][len(a1) >= k]

Long Python

for _ in range(input()):
    n,k = map(int, raw_input().split())
    arr = map(int, raw_input().split())
    count = 0
    for i in arr:
        if i <= 0:
            count += 1
    if count < k:
        print "YES"
        print "NO"

A web developer(Front end and Back end), and DBA at Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me

Leave a reply:

Your email address will not be published.