Codeforces 1692A Marathon Solution & Explanation

Difficulty : 800

Problem Description

You are given four distinct integers a, b, c, d.

Timur and three other people are running a marathon. The value a is the distance that Timur has run and b, c, d correspond to the distances the other three participants ran.

Output the number of participants in front of Timur.

Input

The first line contains a single integer t (1≤t≤104) — the number of test cases.

The description of each test case consists of four distinct integers a, b, c, d (0≤a,b,c,d≤104).

Output

For each test case, output a single integer — the number of participants in front of Timur.

Examples

Input4
2 3 4 1
10000 0 1 2
500 600 400 300
0 9999 10000 9998
Output2
0
1
3

Note

For the first test case, there are 2 people in front of Timur, specifically the participants who ran distances of 3 and 4. The other participant is not in front of Timur because he ran a shorter distance than Timur.

For the second test case, no one is in front of Timur, since he ran a distance of 10000
while all others ran a distance of 0, 1, and 2 respectively.

For the third test case, only the second person is in front of Timur, who ran a total distance of 600 while Timur ran a distance of 500.


Solution

There are several ways to solve this question, and their efficiencies are actually quite similar.

C# Solution

Solution1 – for loop

int t = int.Parse(Console.ReadLine());

for(int i=0; i<t; i++){
    int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    int cnt = 0;
    int timur = arr[0];
    
    for(int j=1;j<arr.Length;j++){
        if(arr[j]>timur){
            cnt++;
        }
    }
    Console.WriteLine(cnt);
}

Solution2

int t = int.Parse(Console.ReadLine());

for(int i=0; i<t; i++){
    int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    int cnt = 0;
    
    cnt+=arr[1]>arr[0]?1:0;
    cnt+=arr[2]>arr[0]?1:0;
    cnt+=arr[3]>arr[0]?1:0;
    
    Console.WriteLine(cnt);
}

Because each array only contains 4 elements, it’s possible to handle the comparisons three times (ternary inequality) without needing a for loop.

Solution3 – LINQ

int t = int.Parse(Console.ReadLine());

for(int i=0; i<t; i++){
    int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    int timur = arr[0];
    
    var cnt = arr.Count(x=>x>timur);
    
    Console.WriteLine(cnt);
}

Easily compare and count by LINQ~

Klook.com

Conclusion

Rather than running a marathon, I hope to complete a 5 km road race first XD

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, 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 – 1692A – Codeforces

Random Codeforces Posts

Leave a Reply

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