# Program to count how many swimmers will win the final match in Python

PythonServer Side ProgrammingProgramming

Suppose we have a list of numbers called nums whose length is n. The elements present in this list are representing the current score of swimmers in a competition. For the final match the first place winner for this current round will get n scores, the second place winner will get n-1 points and so on. We have to check the number of swimmers that can still win the competition in the final round after the current round. If there is a tie for the first in points, that will also be counted as winning.

So, if the input is like nums = [9, 6, 11, 12], then the output will be 3, as the swimmers who are currently at score 9, 11 and 12, they can all win if final score is [13, 9, 13, 13]. That is, the swimmer with score 9 got the first place so got 4 points extra, then 6 point swimmer got the second place so now the score is 9. The 11 point swimmer gets third so new score is 13, and 12 point swimmer got the last place so the score is 12 also. But even if the 6 point swimmer gets first place his final score will be 10 points, 9 point swimmer will get second then his score will be 12 and so on, then also there is no chance of winning for the second swimmer.

To solve this, we will follow these steps −

• if nums is empty, then
• return 0
• n := size of nums
• ans := 0
• sort the list nums
• a := 0
• for i in range n - 1 to 0, decrease by 1, do
• cand := nums[i] + n - i
• if cand > a, then
• a := cand
• for each x in nums, do
• if x + n >= a, then
• ans := ans + 1
• return ans

## Example

Let us see the following implementation to get better understanding −

def solve(nums):
if not nums:
return 0
n = len(nums)
ans = 0
nums.sort()
a = 0
for i in range(n - 1, -1, -1):
cand = nums[i] + n - i
if cand > a:
a = cand
for x in nums:
if x + n >= a:
ans += 1
return ans

nums = [9, 6, 11, 12]
print(solve(nums))

## Input

[9, 6, 11, 12]


## Output

3
Published on 14-Oct-2021 12:01:04