HackerRank Python: Find the Runner-Up Score! Solution

Table of Contents

Question

Given the participants’ score sheet for your University Sports Day, you are required to find the runner-up score. You are given  scores. Store them in a list and find the score of the runner-up.

Input Format

The first line contains n . The second line contains an array  A[ ]  of n  integers each separated by a space.

 

Constraints

  •  2 ≤  n ≤  10
  • -100 ≤  A[i] ≤ 100

Output Format

Print the runner-up score.

Sample Input 0

5
2 3 6 6 5

Sample Output 0

5

Explanation 0

Given list is [2, 3, 6, 6, 5] . The maximum score is 6 , second maximum is 5 . Hence, we print 5 as the runner-up score.

Solution

				
					 n = int(input())
scores = list(map(int, input().split()))

# Remove duplicates and sort the scores in descending order
unique_scores = sorted(set(scores), reverse=True)

# The runner-up score is the second highest score
runner_up_score = unique_scores[1]

print(runner_up_score)

				
			

The code reads the number of scores, n, from the input and then reads the scores themselves as a list, scores. It uses the map() function to convert the input string of space-separated integers into a list of integers.

Next, it removes duplicates from the scores list by converting it to a set and then sorts the unique scores in descending order using the sorted() function with the reverse=True parameter.

Finally, it retrieves the second highest score from the sorted unique scores list (unique_scores[1]) and prints it as the runner-up score.

.

If you find anything wrong in this Solution, feel free to reach us in the comment section.

Sharing Is Caring:

Leave a Comment