Programming Challenge #5

algorithm
programmingchallenge

(oaktree) #1

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

First take in T, the number of cases.

For each case, there will be two lines of input. The first line has N, the number of students in the class, followed by a space and then. K the number of students needed to be present when class starts.

The second line of each case is N space-separated integers, representing the arrival time of each student. Non-positive ( <= 0) arrival times denote that a particular student arrived early or on time; positive arrival times represent how many minutes late a student was to class. On time is not late.


Output

Print YES for each case if the class is canceled; print NO otherwise.


Example

Input

2
4 3
-1 -3 4 2
4 2
0 -1 2 1

Output

YES
NO

For the first case, K = 3 so three students must be present at the start of class for it to remain un-canceled. Only two show up, so the class is canceled.

For the second, Two students must be present at the start, and two are, so class continues.

I’ll post a solution in one day.

problem source here


(Command-Line Ninja) #2

Hmmm this looks interesting! I’ll need to do this when I have more time!

EDIT: I’ve done it, and heres my solution

Not exactly super brief, but it works! Took me about 30 minutes.


#3

That was a good fun 5 mins to do this morning :smiley:


(oaktree) #4

Solutions:


Ruby

#!/bin/ruby
t = gets.strip.to_i
t.times do |x|
    n,k = gets.strip.split(" ").map(&:to_i)
    arr = gets.strip.split(" ").map(&:to_i)
    here = 0
    arr.each do |arrival_time|
        here += 1 if arrival_time <= 0
    end
    puts (here < k ? "YES" : "NO")
end

C++

#include <iostream>
using namespace std;
int main(){
    int t; cin >> t;
    while (t-- > 0) {
        int n,k; cin >> n >> k;
        
        int here = 0, tmp;
        for (int i = 0; i < n; i++) {
            cin >> tmp;
            if (tmp <= 0) here++;
        }
        cout << (here < k ? "YES" : "NO") << endl;
    }
    return 0;
}

C

#include <stdio.h>

int main(){
    int t; scanf("%d",&t);
    while(t-- > 0) {
        int n,k; scanf("%d %d", &n,&k);
        int here = 0, tmp;
        for (int i = 0; i < n; i++) {
            scanf("%d", &tmp);
            if (tmp <= 0) here++;
        }
        printf("%s\n", (here < k ? "YES" : "NO"));
    }
    return 0;
}

Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        while (t-- > 0) {
            int n = in.nextInt();
            int k = in.nextInt();
            int here = 0, tmp;
            for(int a_i=0; a_i < n; a_i++){
                tmp = in.nextInt();
                if (tmp <= 0)
                    here++;
            }
            if (here < k) System.out.println("YES");
            else System.out.println("NO");
        }
    }
}

(pico) #5

Perl

#!/usr/bin/perl

<>;
while (($n, $k) = split (/\s+/, <>))
  {
    @d = split (/\s+/, <>);
    die "Malformed line\n" if ($#d != --$n);
    for ($i = 0, $res =0; $i <= $#d && $res < $k; $i++)
      {
	$res++ if ($d[$i] < 1);
      }

    print (($res >= $k) ? "NO\n" : "YES\n" );
  }

I haven’t used foreach so I can add the condition for early leaving the loop


#6

Python

#!/usr/bin/env python

is_off = []
for i in range(int(raw_input())):
    N, K = [int(i) for i in raw_input().split()]
    here = sum(1 for i in raw_input().split() if int(i) <= 0)
    is_off.append('YES' if here < K else 'NO')
print '\n'.join(is_off)

(oaktree) #7

This topic was automatically closed after 30 days. New replies are no longer allowed.