Input Format
The first line contains . The second line contains an array of integers each separated by a space.
Constraints
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 . The maximum score is , second maximum is . Hence, we print as the runner-up score.
Find the Runner-Up Score! - Hacker Rank Solution
There are many ways to solve this problem.
- This can be solved by maintaining two variables and . Iterate through the list and find the maximum and store it. Iterate again and find the next maximum value by having an if condition that checks if it's not equal to first maximum.
- Create a counter from the given array. Extract the keys, sort them and print the second last element.
- Transform the list to a set and then list again, removing all the duplicates. Then sort the list and print the second last element.
Problem Setter's code:
Python 2
if __name__ == '__main__':
n = int(raw_input())
arr = map(int, raw_input().split())
m1 = max(arr)
m2 = -9999999999
for i in range(n):
if arr[i] != m1 and arr[i] > m2:
m2 = arr[i]
print m2
from collections import Counter
if __name__ == '__main__':
n = int(raw_input())
arr = Counter(map(int, raw_input().split())).keys()
arr.sort()
print arr[-2]
from collections import Counter
if __name__ == '__main__':
n = int(raw_input())
arr = list(set(map(int, raw_input().split())))
arr.sort()
print arr[-2]
please explain the code
ReplyDeleteIn the last example, the input is first converted to a set, which removes duplicates, and then into a list.
DeleteThe list is then sorted with arr.sort().
Finally, the second from last index is printed with arr[-2]
why do we need to remove duplicates?
DeleteIf you don't remove duplicates after sorting it will become like
Delete[2 3 5 6 6]
Arr(-2) gives us second most last Index that means u get 6 so you need to remove duplicates to get correct output
can u please explain what is the need of n why are we asking for input of n can't we just take arr and continue i have tried it and the results are identical
Deleten=int(input())
ReplyDeletearr= map(int,input().split())
arr=list(arr)
arr.sort(reverse=True)
for i in range(1,n):
if (arr[0]==arr[i]):
continue
print(arr[i])
break
Can you please explain
Deleteplease tell me about "(reverse=True)"
ReplyDeleteinorder to print in reverse order,we use this condition
Delete*sort in
DeleteTraceback (most recent call last):
DeleteFile "Solution.py", line 6, in
for i in range(list,n):
TypeError: 'type' object cannot be interpreted as an integer
why did we take m2=-9999999999
ReplyDeletewhy did we take m2=-9999999999
ReplyDeleteBy default reverse have false value to do reverse or in descending order of list elements we have set it to reverse =true
ReplyDeleteworking solution in python -
ReplyDeletearr=list(arr)
arr.sort(reverse=True)
for i in range(1,n):
if arr[i] != arr[0]:
print(arr[i])
break
the moment it encounters first arr[[i] != arr[0] it will print it and break out of the loop. if you don't break the loop, it will keep on printing all the integers smaller than a[0]
a = input()
ReplyDeletestr_lst = a.split()
lst = []
for i in str_lst:
lst.append(int(i))
lst.sort()
max = lst[-1]
key =0
for i in lst:
if(i > key and i < max):
key = i
print(key)
why this code is not working it is working fine online compilers and pycharm
if __name__ == '__main__':
ReplyDeleten = int(input())
arr = map(int, input().split())
arr = set(arr)
arr = list(arr)
arr.sort(reverse=True)
for i in range(1,n):
if (arr[0]==arr[i]):
continue
print(arr[1])
break
if __name__ == '__main__':
ReplyDeleten = (int(input()))
arr = map(int, input().split())
arr=list(arr)
arr=set(arr)
score=max(arr)
arr.remove(score)
H_run=max(arr)
print(H_run)
if __name__ == '__main__':
ReplyDeleten = int(input())
arr = map(int, input().split())
arr = set(arr)
a1 = list(arr)
a1.sort()
print(a1[-2])
if __name__ == '__main__':
ReplyDeleten = int(input())
arr = map(int, input().split())
arr=set(arr)
arr = list(arr)
arr.sort()
print(arr[-2])
n = int(input())
ReplyDeletearr = map(int, input().split())
arr=set(arr)
print(sorted(arr,reverse=True)[1])
n = int(input())
ReplyDeletearr = map(int, input().split())
arr1 = list(arr)
arr1 = [x for x in arr1 if x != max(arr1)]
print(max(arr1))
arr = []
ReplyDeleteT = int(input())
arr=map(int,input().split())
arr=list(arr)
arr.sort()
res = []
for i in arr:
if i not in res:
res.append(i)
print(res[-2])
n=int(input())
ReplyDeletels=set(input().split())
save=max(ls)
ls.discard(max(ls))
if len(ls) == 0:
print(save)
else:
print(max(ls))
where is the error?
if __name__ == '__main__':
ReplyDeleten = int(input())
arr = map(int, input().split())
# Sorting_func()
#print(sorted(list(set(arr)))[-2])
def Sorting_func(arr):
i = set(arr)
list_1 = list(i)
list_1.sort()
return list_1[-2]
print(Sorting_func(arr))