- 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

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

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))

[9, 6, 11, 12]

3

- Related Questions & Answers
- Python Program to find out how many times the balls will collide in a circular tube
- Program to find how many years it will take to reach t amount 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
- How many ways are there to initialize a final variable in java?
- Count how many rows have the same value in MySQL?
- Program to count how many times we can find "pizza" with given string characters in Python
- Python Program to find out how many cubes are cut
- How you will create your first program in Python?
- Count how many times the substring appears in the larger String in Java
- Program to find how many ways we can climb stairs in Python
- What will happen when we try to override final method of the superclass in Java?
- Program to find out how many boxes can be put into the godown in Python
- Program to find how many swaps needed to sort an array in Python
- Program to Find Out the Maximum Final Power of a List in Python

Advertisements