Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1553
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1553
Frequent Asked Questions
By Cristhian Bonilha, UTFPR Brazil
Timelimit: 1
Lots of sites in the internet have a page called “Frequent Asked Questions” that, as the name itself says, contains the questions that the users make frequently.
The URI portal is used to receive lots of questions from their users, so Neilor decided that it would be a good idea to add a Frequent Asked Questions page to the site. As Neilor is very busy these days, he asked you to help him with this page.
Given the identifiers of the questions asked by the users, say the number of questions that will be added to this new page of the site. A question is considered “frequent” when it is asked at least K times.
Input
There will be several test cases. Each test case starts with two integers N and K (1 ≤ N ≤ 1000, 1 ≤ K ≤ 100), indicating the number of asked questions, and the number of times a question must be asked to be considered “frequent”, respectively.
Following there will be N integers P (1 ≤ P ≤ 100), each indicating the number of a question asked.
The last test case is indicated when N = K = 0, which should not be processed.
Output
For each test case print one line, containing one integer, indicating the number of questions that will be added to this new page of the site.
Sample Input | Sample Output |
5 2 1 4 2 1 3 5 2 1 1 2 3 2 8 3 1 1 3 5 4 6 3 4 8 3 1 1 1 1 1 1 1 1 0 0 |
1 2 0 1 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main(void) {
int n, k, i, j, q, e, v[1000];
while (scanf("%d %d", &n, &k) == 2 && (n != 0 || k != 0)) {
for (i = 0; i < n; ++i) scanf("%d", &v[i]);
q = 0;
for (i = 1; i < 101; ++i) {
e = 0;
for (j = 0; j < n; ++j)
if (i == v[j]) ++e;
if (e >= k) ++q;
}
printf("%d\n", q);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
1 4 2 1 3
5 2
1 1 2 3 2
8 3
1 1 3 5 4 6 3 4
8 3
1 1 1 1 1 1 1 1
0 0
Output
2
0
1
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
using namespace std;
int main(void) {
int n, k, i, j, q, e, v[1000];
while (cin >> n >> k && (n != 0 || k != 0)) {
for (i = 0; i < n; ++i) cin >> v[i];
q = 0;
for (i = 1; i < 101; ++i) {
e = 0;
for (j = 0; j < n; ++j)
if (i == v[j]) ++e;
if (e >= k) ++q;
}
cout << q << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
1 4 2 1 3
5 2
1 1 2 3 2
8 3
1 1 3 5 4 6 3 4
8 3
1 1 1 1 1 1 1 1
0 0
Output
2
0
1
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
static Reader in = new Reader(System.in);
static Writer out = new Writer(System.out);
public static void main(String[] args) throws IOException {
int N, K, sol;
while (true) {
N = in.nextInt();
K = in.nextInt();
if(N == 0 && K == 0){
break;
}
sol = 0;
int[] F = new int[1001];
while (N-- > 0) {
F[in.nextInt()]++;
}
for (int Fi : F) {
if(Fi >= K){
sol++;
}
}
out.println(sol);
}
in.close();
out.flush();
out.close();
}
static class Reader implements Closeable {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public Reader(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
private StringTokenizer getTokenizer() throws IOException {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = nextLine();
if (line == null) {
return null;
}
tokenizer = new StringTokenizer(line);
}
return tokenizer;
}
public boolean hasNext() throws IOException {
return getTokenizer() != null;
}
public String next() throws IOException {
return hasNext() ? tokenizer.nextToken() : null;
}
public String nextLine() throws IOException {
tokenizer = null;
return reader.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public String[] nextStringArray(int size) throws IOException {
String[] array = new String[size];
for (int i = 0; i < size; i++) {
array[i] = next();
}
return array;
}
public int[] nextIntArray(int size) throws IOException {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = nextInt();
}
return array;
}
public long[] nextLongArray(int size) throws IOException {
long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = nextLong();
}
return array;
}
public double[] nextDoubleArray(int size) throws IOException {
double[] array = new double[size];
for (int i = 0; i < size; i++) {
array[i] = nextDouble();
}
return array;
}
@Override
public void close() throws IOException {
tokenizer = null;
reader.close();
}
}
static class Writer {
private final PrintWriter writer;
public Writer(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush(>;
}
}
}
Copy The Code &
Try With Live Editor
Input
1 4 2 1 3
5 2
1 1 2 3 2
8 3
1 1 3 5 4 6 3 4
8 3
1 1 1 1 1 1 1 1
0 0
Output
2
0
1
#4 Code Example with Python Programming
Code -
Python Programming
while True:
e = str(input()).split()
n = int(e[0])
k = int(e[1])
if n == k == 0: break
e = [int(x) for x in str(input()).split()]
p = 0
for i in range(1, 101):
if e.count(i) >= k:
p += 1
print(p)
Copy The Code &
Try With Live Editor
Input
1 4 2 1 3
5 2
1 1 2 3 2
8 3
1 1 3 5 4 6 3 4
8 3
1 1 1 1 1 1 1 1
0 0
Output
2
0
1