Find Second Largest Element
EasyPractice library · +10 XParrayssortingloops
Given an array of N distinct integers, find and print the second largest element.
Input
The first line contains a single integer N. The second line contains N space-separated integers.
Output
Print a single integer, the second largest element in the array.
Constraints
1 <= N <= 1000; -10^9 <= A[i] <= 10^9; All elements in the array are distinct.
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
5 1 5 2 8 3
Output
5
The elements are 1, 5, 2, 8, 3. The largest is 8, the second largest is 5.
Example 2
Input
3 10 20 5
Output
10
The elements are 10, 20, 5. The largest is 20, the second largest is 10.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 1 5 2 8 3
Example 2 input
3 10 20 5