Hanwool Codes RSS Tag Admin Write Guestbook
2023-01-29 22:03:44

Find the Runner-Up Score! | HackerRank

 

Find the Runner-Up Score! | HackerRank

For a given list of numbers, find the second largest number.

www.hackerrank.com

 

Goal

 

1. Give n scores

2. Store them in a list

3. find the score of the runner-up

 

so, you should print the runner-up score.

 

Solution

 

You can convert map into list

After then sort asending order and then convert list to set because you need to escape duplicate scores

You can print runner-up score with index -2 because negative indexing is used in Python to begins slicing from the end of list. So in our case, you need to read index -2.

# Find the Runner-Up Score!

if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())
    
    list_arr = list(arr)
    list_arr.sort()
    
    convert_arr = list(set(list_arr))
    
    print(convert_arr[-2])
Hanwool Codes. Designed by 코딩재개발.