DEV Community

Cover image for Find the runner up score - HackerRank Python Solution
Deepak Raj
Deepak Raj

Posted on • Originally published at codeperfectplus.com on

1

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.

Find the runner up score Solution in Python

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

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.

Problem statement is taken from Hackerrank, and the solutions are implemented by CodePerfectPlus team

Other Article By Author

30 Days of Code SubReddit

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay