

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 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
- if x + n >= a, then
- 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
- Related Questions & Answers
- C++ program to count how many ways two players win or make draw in dice throwing game
- Python Program to find out how many times the balls will collide in a circular tube
- C++ program to count minimum how many minutes after there will be no new angry students
- Program to find how many years it will take to reach t amount in Python
- Program to count number of ways to win at most k consecutive games in Python
- Program to count how many ways we can divide the tree into two trees in Python
- Program to count how many ways we can cut the matrix into k pieces in python
- Program to find hoe many children will get candies while distributing them maintaining the rules in Python
- Program to count how many blocks are covered k times by walking in Python
- Program to find how many lines intersect in Python
- How many ways are there to initialize a final variable in java?
- Program to find out if we win in a game in Python
- Count how many rows have the same value in MySQL?
- C++ program to count at least how many operations needed for given conditions
- Program to count how many times we can find "pizza" with given string characters in Python