[HackerRank] All Domains - Python - Basic Data Types - Find the Second Largest Number

2022. 3. 13. 16:21Algorithm

728x90
반응형

You are given  numbers. Store them in a list and find the second largest number.

Input Format 
The first line contains . The second line contains an array [] of  integers each separated by a space.

Constraints 
 

Output Format 
Output the value of the second largest number.

Sample Input

5 2 3 6 6 5 

Sample Output

5

 

------------------------------------------------

 

if __name__ == '__main__':

    n = int(input())

    arr = map(int, input().split())

    p_max, s_max = [-111, -111]

 

    for x in arr:

        if x > p_max and x > s_max:

            s_max = p_max

            p_max = x

        elif x != p_max and x > s_max:

            s_max = x

 

    print(s_max)

------------------------------------------------

728x90
반응형