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

Suppose we have a list of numbers called nums whose length is n. The elements present in this list represent the current scores of swimmers in a competition. For the final match, the first place winner for this current round will get n points, 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 place 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 scores 9, 11 and 12, they can all win if the final score is [13, 9, 13, 13]. That is, the swimmer with score 9 got the first place so got 4 points extra (total 13), the 6-point swimmer got the second place so the new score is 9, the 11-point swimmer gets third place so the new score is 13, and the 12-point swimmer got the last place so the score becomes 13. Even if the 6-point swimmer gets first place, their final score will be 10 points, which is not enough to win.

Algorithm

To solve this problem, we will follow these steps ?

  • If nums is empty, return 0
  • n := size of nums
  • ans := 0 (counter for potential winners)
  • Sort the list nums
  • max_score := 0 (maximum possible final score)
  • For i in range n - 1 to 0, decrease by 1:
    • candidate_score := nums[i] + n - i
    • If candidate_score > max_score, then max_score := candidate_score
  • For each swimmer's score x in nums:
    • If x + n >= max_score, then increment ans
  • Return ans

Example

Let us see the following implementation to get a better understanding ?

def solve(nums):
    if not nums:
        return 0
    
    n = len(nums)
    ans = 0
    nums.sort()
    max_score = 0
    
    # Find the maximum possible final score
    for i in range(n - 1, -1, -1):
        candidate_score = nums[i] + n - i
        if candidate_score > max_score:
            max_score = candidate_score
    
    # Count swimmers who can achieve the maximum score
    for x in nums:
        if x + n >= max_score:
            ans += 1
    
    return ans

# Test with the given example
nums = [9, 6, 11, 12]
print("Input:", nums)
print("Output:", solve(nums))
Input: [9, 6, 11, 12]
Output: 3

How It Works

The algorithm works by first finding the maximum possible final score any swimmer can achieve. It does this by considering each position (1st, 2nd, 3rd, etc.) and calculating what the final score would be if the highest-scoring swimmer took that position.

Then it checks how many swimmers can potentially achieve this maximum score by getting first place (adding n points to their current score).

Step-by-Step for Example [9, 6, 11, 12]

  1. After sorting: [6, 9, 11, 12]
  2. Calculate maximum possible scores:
    • Position 4 (last): 12 + 4 - 4 = 12
    • Position 3: 11 + 4 - 3 = 12
    • Position 2: 9 + 4 - 2 = 11
    • Position 1 (first): 6 + 4 - 1 = 9
  3. Maximum score is 12
  4. Check who can reach 12+ by getting first place:
    • 6 + 4 = 10 (No)
    • 9 + 4 = 13 (Yes)
    • 11 + 4 = 15 (Yes)
    • 12 + 4 = 16 (Yes)
  5. Result: 3 swimmers can win

Conclusion

This solution efficiently determines how many swimmers can potentially win by calculating the maximum achievable final score and checking which swimmers can reach that score by placing first. The time complexity is O(n log n) due to sorting.

Updated on: 2026-03-26T16:44:57+05:30

434 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements