Second Largest Number Python

PROGRAM TO PRINT THE SECOND LARGEST NUMBER


def print2largest(arr, arr_size):
  
    # There should be atleast
        # two elements 
    if (arr_size < 2):
      
        print(" Invalid Input ")
        return
      
  
    first = second = -2147483648
    for i in range(arr_size):
      
        # If current element is
                # smaller than first
        # then update both
                # first and second 
        if (arr[i] > first):
          
            second = first
            first = arr[i]
          
  
        # If arr[i] is in
                # between first and 
        # second then update second 
        elif (arr[i] > second and arr[i] != first):
            second = arr[i]
      
    if (second == -2147483648):
        print("There is no second largest element")
    else:
        print("The second largest element is", second)

arr = [12, 35, 1, 10, 34, 1]
n = len(arr)
  
print2largest(arr, n)


OUTPUT
The second largest element is 34


Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java