Saturday, February 23, 2019

Find the Runner-Up Score! - Hacker Rank Solution

Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given scores. Store them in a list and find the score of the runner-up.
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.
  1. 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.
  2. Create a counter from the given array. Extract the keys, sort them and print the second last element.
  3. 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]

25 comments:

  1. Replies
    1. In the last example, the input is first converted to a set, which removes duplicates, and then into a list.

      The list is then sorted with arr.sort().

      Finally, the second from last index is printed with arr[-2]

      Delete
    2. why do we need to remove duplicates?

      Delete
    3. If you don't remove duplicates after sorting it will become like
      [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

      Delete
    4. 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

      Delete
  2. n=int(input())
    arr= 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

    ReplyDelete
  3. Replies
    1. inorder to print in reverse order,we use this condition

      Delete
    2. Traceback (most recent call last):
      File "Solution.py", line 6, in
      for i in range(list,n):
      TypeError: 'type' object cannot be interpreted as an integer

      Delete
  4. why did we take m2=-9999999999

    ReplyDelete
  5. By default reverse have false value to do reverse or in descending order of list elements we have set it to reverse =true

    ReplyDelete
  6. working solution in python -
    arr=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]

    ReplyDelete
  7. a = input()
    str_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

    ReplyDelete
  8. if __name__ == '__main__':
    n = 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

    ReplyDelete
  9. if __name__ == '__main__':
    n = (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)

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

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

    ReplyDelete
  12. n = int(input())
    arr = map(int, input().split())
    arr=set(arr)
    print(sorted(arr,reverse=True)[1])

    ReplyDelete
  13. n = int(input())
    arr = map(int, input().split())
    arr1 = list(arr)
    arr1 = [x for x in arr1 if x != max(arr1)]

    print(max(arr1))

    ReplyDelete
  14. arr = []
    T = 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])

    ReplyDelete
  15. n=int(input())
    ls=set(input().split())
    save=max(ls)
    ls.discard(max(ls))
    if len(ls) == 0:
    print(save)
    else:
    print(max(ls))
    where is the error?

    ReplyDelete
  16. if __name__ == '__main__':
    n = 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))

    ReplyDelete

Powered by Blogger.