Next Round

Next Round
Codeforces easy

Given the scores of \(N\) participants in some competition, your task is to count how many scored greater than or equal to the participant that ranked \(K\)-th place and have a positive score.

Caution

Its a common mistake to forget to exclude the participants that scored \(0\), you must follow the statement exactly and read the task again if you can’t find the mistake in your idea.

Tip

It is given in the task that \(A_{i-1} \ge A_i\). This is an essential fact we will use in the solution.

Solution

The \(K\)-th ranking person is in index \(K - 1\) of the array. So it is sufficient to loop over the array and count the indices with value greater than or equal to that index.

#include<iostream>
using namespace std;

int main () {
    int n, k;
    cin >> n >> k;

    int a[n], ans = 0;

    for (int i = 0 ; i < n ; i++) cin >> a[i];

    for (int i = 0 ; i < n ; i++) {
        if (a[i] >= a[k - 1] && a[i] > 0) ans++;
    }

    cout << ans;
}
Caution

We must read the full array before processing because otherwise if i < k - 1 then a[k - 1] will be read yet.