Hello coders, today we will be solving Find the Runner-Up Score! in Python Hacker Rank Solution.
Problem
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
5
Explanation 0
Given list is [2, 3, 6, 6, 5]. The maximum score is 6, second maximum score is 5. Hence, we print 5 as the runner-up score.
Solution – Find the Runner-Up Score! in Python – Hacker Rank Solution
if __name__ == '__main__': n = int(input()) arr = map(int, input().split()) print(sorted(list(set(arr)))[-2])
Disclaimer: The above Problem (Find the Runner-Up Score!) is generated by Hacker Rank but the Solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.
it is only for the given output .it does not apply for all the test case…
no it will work for all becoz the set will remove the duplicate value so the another 6 will be vanished
Your are right
Thank you for the solution…
why it is -2 at last
to find second highest number
when we sort it default sort in ascending order
to access to -2
from last second value
n = int(input())
arr=[]
for i in range(1,n+1):
val=input()
arr.append(val)
arr.sort()
print(arr[n-2])
I have written this code in IDLE and it works and when I pasted this on Hacer rank it gives error
sir take a condition when last two elements are same (after sorting ) then n-2 return the last~largest no. ,, not the 2nd last
better to apply if else condition to overcome this condition
def an(n,arr):
a=max(arr)
arr.remove(a)
return max(arr)
if __name__ == ‘__main__’:
n = int(input())
arr = map(int, input().split())
print(an(n,arr))
its working in other idle but not in hacker rank. why..?
if __name__ == ‘__main__’:
n = int(input())
arr = map(int, input().split())
runner_up = list(set(arr));
runner_up.sort()
print(runner_up[-2]);
this will works for all.
Non ascii input error in hackerrank
#this will work fine “we 1st find max of array then count the occurrence of maximum number “if occurrence of max number is 2 for loop will run 2 times and remove the mamx numebr and at last we print the max of array
if __name__ == ‘__main__’:
n = int(input())arr = list(map(int, input().split()))
maximum=max(arr)
count=arr.count(maximum)
for i in range (count):
arr.remove(maximum)
print(max(arr))
same problem
n = int(input(“”))
list = []
for i in range(n):
N = int(input(“”))
list.append(N)
list.sort()
print(list[-2])
It works on other softwares but it showing error in hacker rank