HackerRank Solution: Find the Runner-up Score! [4 Methods]


Written By - Bashir Alam
Advertisement

Question: Find the Runner-up Score [Python Basic Data Types]

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:

Advertisement
5

Explanation 0:

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

 

Possible solutions

Now let us discuss the possible solutions. The following code is already given in the editor of the hacker rank:

if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())

Let us now move toward the solutions:

 

Solution-1: Using map function

Now let us solve the problem using map function:

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

This code takes in user input for a variable "n" and a list of integers "arr" separated by spaces. It then creates a new list "arr2" by converting the input list to a set, which removes duplicates and then sorts the set. The final line of code then prints the second to last element of the sorted, unique list "arr2". This code finds the second largest element in the input list "arr" by first removing duplicates and then sorting the list and printing the second to last element in the list.

 

Solution-2: Using for and while loop

Now we will use while loop and  for loop to solve the problem:

if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())
    l = []
    for i in arr:
        l.append(i)
    mx = max(l)
    a = []
    while mx in l:    
        l.remove(mx)
    print(max(l))

This code takes in user input for a variable "n" and a list of integers "arr" separated by spaces. It then creates an empty list "l" and iteratively appends each element of "arr" to it. It then finds the maximum value of the list "l" and assigns it to variable "mx". It then creates an empty list "a" and enters a while loop that checks if the maximum value "mx" is still present in the "l" and removes it as long as it is present in it. After the while loop is done, the final line of code prints the new maximum value of "l" which would be the second largest element in the original input list "arr" after removing the largest element from it.

Advertisement

 

Solution-3: Solution in one line

We can now solve the same problem in one line as well.

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

This code takes the input of a list of integers "arr" separated by spaces, assigns it to an anonymous function that converts the input list to a set, removes the duplicate elements, converts it back to the list, sorts it, and assigns the second largest value of the sorted list to the variable print() and prints the second largest element. This code is a more concise version of the previous examples, it performs the same task but with fewer lines and more compact expressions. It uses the power of chaining function calls together, it takes the input and converts it to a set, removes the duplicates, converts it back to a list, sorts it, and finally gets the second largest element of the sorted list and prints it out.

 

Solution-4: Using conditional statements

We will now use the if-else statements to solve the problem:

if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())
    arr2 = sorted(arr, reverse=True)
    for i,x in enumerate(arr2):
        if max(arr2)==arr2[i]:
            pass
        else:
            print(x)
            break

Similar to other solutions, this code also takes in user input for a variable "n" and a list of integers "arr" separated by spaces. It then creates a new list "arr2" by sorting the input list in descending order. The code then uses a for loop with the enumerate function to iterate through the elements of the sorted list "arr2" with the index and value. Inside the for loop, it checks if the current element is the max element of the list, if so it passes the loop and continues to the next element, otherwise it prints the current element and then uses the break statement to exit the loop. This code finds the second largest element in the input list "arr" by first sorting it in descending order and then iterating through the elements of the sorted list, checking if the current element is the max element, if not it prints the current element and exits the loop, thus the second largest element is printed

 

Summary

In this short article, we discussed how we can solve Find the runner-up score problem on hacker rank. Here we discussed 4 different methods to solve the problem and we explained each solutions.

 

Further Reading

Question on Hacker Rank: Find the Runner-Up Score! [Python Basic Data Types]

Advertisement

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment