1 min to read
Find the runner up score - HackerRank Python Solution
In this tutorial, we will guide you through the process of solving the Find the runner up score programming problem from HackerRank’s “Python” domain.
Disclaimer: We encourage you to solve this challenge yourself before reading our tutorial. We have provided a detailed explanation of the problem and our solutions to help you check your work.
Problem Statement
Find the runner-up score in the given student score list or in simple words find the second maximum number in the array. There are n
student scores in the given list. we have to find the runner-up score in the given list. The runner-up score is the second maximum number in the list.
Suppose the given score for 5 students is 91, 92, 93, 94, 95. The runner-up score should be 94. Because 94 is the second maximum number in the list. It’s a useful problem to understand the list and sort concept in Python.
Find the runner up score Solution in Python
Explanation
In the above code, we have created a function named find_runner_up_score()
that takes a list of numbers as input and returns the second largest number in the list.
- First, we used the set() function to remove duplicate elements from the list.
- then we use the sorted() function to sort the list in ascending order.
- then we use the [-2] index to access the second largest number in the list.
Time Complexity
The time complexity of the above solution is O(n log n) because we are using the sorted() function to sort the list.