Find the Runner-Up Score! in Python | HackerRank Solution

Hello coders, today we will be solving Find the Runner-Up Score! in Python Hacker Rank Solution.

Find the Runner-Up Score! in Python - Hacker Rank Solution

Problem

Given the participants’ score sheet for your University Sports Day, you are required to 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 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 score is 5. Hence, we print 5 as the runner-up score.

Solution – Find the Runner-Up Score! in Python – Hacker Rank Solution

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

Disclaimer: The above Problem (Find the Runner-Up Score!) is generated by Hacker Rank but the Solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

15 thoughts on “Find the Runner-Up Score! in Python | HackerRank Solution”

    1. no it will work for all becoz the set will remove the duplicate value so the another 6 will be vanished

    1. to find second highest number
      when we sort it default sort in ascending order
      to access to -2
      from last second value

  1. n = int(input())
    arr=[]
    for i in range(1,n+1):
    val=input()
    arr.append(val)
    arr.sort()
    print(arr[n-2])

    I have written this code in IDLE and it works and when I pasted this on Hacer rank it gives error

    1. sir take a condition when last two elements are same (after sorting ) then n-2 return the last~largest no. ,, not the 2nd last

      better to apply if else condition to overcome this condition

    2. def an(n,arr):
      a=max(arr)
      arr.remove(a)
      return max(arr)

      if __name__ == ‘__main__’:
      n = int(input())
      arr = map(int, input().split())
      print(an(n,arr))

    1. if __name__ == ‘__main__’:
      n = int(input())
      arr = map(int, input().split())
      runner_up = list(set(arr));
      runner_up.sort()
      print(runner_up[-2]);

      this will works for all.

  2. Shehryar khan

    #this will work fine “we 1st find max of array then count the occurrence of maximum number “if occurrence of max number is 2 for loop will run 2 times and remove the mamx numebr and at last we print the max of array
    if __name__ == ‘__main__’:
    n = int(input())arr = list(map(int, input().split()))

    maximum=max(arr)
    count=arr.count(maximum)
    for i in range (count):
    arr.remove(maximum)
    print(max(arr))

  3. n = int(input(“”))
    list = []
    for i in range(n):
    N = int(input(“”))
    list.append(N)
    list.sort()
    print(list[-2])

    It works on other softwares but it showing error in hacker rank

Leave a Comment

Your email address will not be published. Required fields are marked *