Find the Runner-Up Score Python HackerRank Solution

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

Code:

if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    arr.sort(reverse=True)
   
    for i in range(len(arr)):
        if arr[i]!=arr[i+1]:
            break
        else:
            continue
    print(arr[i+1])

This is a Python program that takes an integer n and a list of integers arr as input, and prints the second highest number in the list. It is a solution to the “Find the Runner-Up Score!” problem on Hackerrank.

The program first reads in an integer n using the input() function and converts it to an integer using the int() function.

The program then reads in a list of n integers arr using the input() function and map() function with int as the first argument.

The split() function is used to split the input string at each whitespace character, and the resulting string values are mapped to integer values using the int() function.

The list() function is then used to convert the resulting map object into a list.

The program then sorts the list of integers in descending order using the sort() method with the reverse=True argument.

The program then uses a for loop to iterate over each element in the sorted list. Inside the loop, the program checks if the current element is not equal to the next element in the list.

If this condition is true, the program breaks out of the loop. If the condition is false, the loop continues to the next iteration.

After the loop, the program prints the element at the index i+1. The variable i is the index of the last element in the sorted list that is equal to the highest element in the list.

Since the list is sorted in descending order, the second highest element in the list is at index i+1.

The purpose of this program is to find the second highest number in a list of integers, given the number of integers in the list and the list itself.

Leave a Comment