Codeforces 158A Next Round Solution & Explanation

Difficulty : 800

Problem Description

“Contestant who earns a score equal to or greater than the k-th place finisher’s score will advance to the next round, as long as the contestant earns a positive score…” — an excerpt from contest rules.

A total of n participants took part in the contest (n ≥ k), and you already know their scores. Calculate how many participants will advance to the next round.

Input

The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 50) separated by a single space.

The second line contains n space-separated integers a1, a2, …, an (0 ≤ ai ≤ 100), where ai is the score earned by the participant who got the i-th place. The given sequence is non-increasing (that is, for all i from 1 to n - 1 the following condition is fulfilled: ai ≥ ai + 1).

Output

Output the number of participants who advance to the next round.

Examples

Input8 5
10 9 8 7 7 7 5 5
Output6
Input4 2
0 0 0 0
Output0

Note

In the first example the participant on the 5th place earned 7 points. As the participant on the 6th place also earned 7 points, there are 6 advancers.

In the second example nobody got a positive score.


Solution

C# Solution

⚠️Solution1 – without checking number

string[] first = Console.ReadLine().Split(' '); //split by space
int n = int.Parse(first[0]); 
int k = int.Parse(first[1]); 

string[] ans = Console.ReadLine().Split(' '); //split by space
string chk = ans[k-1];
int add = 0;

if(chk=="0"){
    for(int i=0;i<k;i++)
    {
        if(ans[i]!="0")
        {
            add+=1;
        } else {
            break;
        }
    }
} else{
    add = k;
    
    for(int i=k;i<n;i++)
    {
        if(ans[i]==chk)
        {
            add+=1;
        }
    }   
}
Console.WriteLine(add); 

In my first solution, I solve it without using converting the score to number.

Based on the score of the k-th contestant (chk), the program performs different actions:

  1. If the score of the k-th contestant is 0 (chk==”0″), the program iterates through the scores of the first k contestants and counts the number of contestants with non-zero scores, storing it in the variable add. If it encounters a contestant with a score of 0, it exits the loop.
  2. If the score of the k-th contestant is not 0, the program sets add to k, indicating that the first k contestants are guaranteed to advance. Then, it iterates through all contestants after the k-th position, counts the number of contestants with scores equal to chk, and adds them to the add variable.

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts or clicking ads, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 158A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *