Instagram
youtube
Facebook
Twitter

Python Find the Runner-Up Score! HackerRank Solution


In this tutorial, we will solve a Hacker rank Find the runner-up problem!

Task

Given the participants’ score sheet for your University Sports Day, you must find the runner-up score. You are given n 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 are 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

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

 

Solution: 

if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    arr1 = set(arr)
    arr2 = sorted(arr1)
    print(arr2[-2])

 

Steps Used in solving the problem -

Step 1: First n will take an integer type input of n scores.

Step 2: then, arr will make a list of these n scores.

Step 3: After this, we converted our list to set so, it will not store multiple same integers.

Step 4: then, I sorted my list of scores.

Step 5: In the last step I printed the second-last integer of my list. And, it is the runner-up score.