Skip to content
thecscience
THECSICENCE

Learn everything about computer science

  • Home
  • Human values
  • NCERT Solutions
  • HackerRank solutions
    • HackerRank Algorithms problems solutions
    • HackerRank C solutions
    • HackerRank C++ solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
thecscience
THECSICENCE

Learn everything about computer science

HackerRank Angry Professor Problem Solution

Yashwant Parihar, April 14, 2023April 15, 2023

In this post, We are going to solve HackerRank Angry Professor Problem. A Discrete Mathematics professor has a class of students. Frustrated with their lack of discipline, the professor decides to cancel a class if fewer than some number of students are present when class starts. Arrival times go from on time (arrival time ≤ 0) to arriving late (arrival Time > 0).

Given the arrival time of each student and a threshold number of attendees, determine if the class is canceled.

Example

n = 5

k = 3

a = [-2, -1, 0, 1, 2]

The first 3 students arrived. The last 2 were late. The threshold is 3 students, so the class will go on. Return YES.

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.

Function Description

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

angryProfessor has the following parameter(s):

  • int k: the threshold number of students
  • int a[n]: the arrival times of the n students

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 a) and the cancellation threshold.
The second line contains n space-separated integers (a[1], a[2],…, a[n]) that describe the arrival times for each student.
Constraints

  • 1≤t≤ 10
  • 1 ≤ n ≤ 1000
  • 1 ≤ k ≤ n
  • -100 ≤ a[i] < 100, where i Є [1,…….n]

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 = 3. The professor wants at least 3 students in attendance, but only 2 have arrived on time (-3 and -1) so 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 arrived on time (0 and -1). The class is not canceled.

HackerRank Angry Professor Problem Solution
HackerRank Angry Professor Problem Solution

Angry Professor C Solution

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int N=0,K=0,count=0;
        scanf("%d %d",&N, &K);
        while(N--)
        {
            int temp=0;
            scanf("%d",&temp);
            if(temp<=0)
            { 
                count++;
            }
        }
        if(count>=K)
        {
            printf("%s\n","NO");
        }
        else
        {
            printf("%s\n","YES");
        }
    }
    return 0;
}

Angry Professor C++ Solution

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int T; cin >> T;
    while (T--) {
        int n, k; cin >> n >> k;
        vector<int> a(n);
        for (int& i : a)
            cin >> i;
        int attend = count_if(a.begin(), a.end(), [&](int i) {return i <= 0;});
        cout << (attend >= k ? "NO\n" : "YES\n");
    }
    return 0;
}

Angry Professor C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Solution {
    static void Main(String[] args) {
        va numberOfTestCases = int.Parse(Console.ReadLine());
        for(int i = 0; i<numberOfTestCases; i++){
            var k = int.Parse(Console.ReadLine().Split(' ')[1]);
            var onTimeCount = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).Count(x => x <= 0);
            Console.WriteLine(onTimeCount < k ? "YES" : "NO");
        }
    }
}

Angry Professor Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'angryProfessor' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts following parameters:
     *  1. INTEGER k
     *  2. INTEGER_ARRAY a
     */

    public static String angryProfessor(int k, List<Integer> a) 
    {
        int count=0;
       for(int i=0;i<a.size();i++)
       {
          if(a.get(i)<=0)
          {
              count++;
          } 
       }
       return ((count>=k)?"NO":"YES");

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int t = Integer.parseInt(bufferedReader.readLine().trim());

        IntStream.range(0, t).forEach(tItr -> {
            try {
                String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

                int n = Integer.parseInt(firstMultipleInput[0]);

                int k = Integer.parseInt(firstMultipleInput[1]);

                List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                    .map(Integer::parseInt)
                    .collect(toList());

                String result = Result.angryProfessor(k, a);

                bufferedWriter.write(result);
                bufferedWriter.newLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Angry Professor JavaScript Solution

function processData(input) {
    var dados = input.split("\n"),
        casos = [],
        alunos = [],
        aux, cont = 0;

    for (var i = 0; i < dados[0]; i++) {
        aux = (i * 2) + 1;
        casos = dados[aux].split(" ");
        alunos = dados[aux + 1].split(" ");
        cont = 0;

        for (var j = 0; j < casos[0]; j++) {
            if (alunos[j] <= 0) {
                cont++;
            }
        }

        if (casos[1] <= cont) {
            console.log("NO");
        }else{
            console.log("YES");
        }
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Angry Professor Python Solution

cases = int(input())
answers = []

for i in range(0, cases):
	(students, minStu) = [int(i) for i in input().split(" ")]
	arrivalTimes = [int(i) for i in input().split(" ")]
	arrivalTimes.sort()

	if arrivalTimes[minStu-1] > 0:
		answers.append("YES")
	else:
		answers.append("NO")
		
for i in answers:
	print(i)

Other Solution

  • HackerRank Beautiful Days at the Movies Solution
  • HackerRank Viral Advertising Problem Solution
c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython

Post navigation

Previous post
Next post

Leave a Reply

You must be logged in to post a comment.

  • HackerRank Dynamic Array Problem Solution
  • HackerRank 2D Array – DS Problem Solution
  • Hackerrank Array – DS Problem Solution
  • Von Neumann and Harvard Machine Architecture
  • Development of Computers
©2025 THECSICENCE | WordPress Theme by SuperbThemes