Accenture Coding Question | Second Largest Number | CodeWindow

Second Largest

Given an array (containing at most 1000 positive integers), find the second largest integer. If there exists no second largest integer, return -1.

Input specification:
Input 1: Length of the array.
Input 2: an array of positive integers.

Output Specification:
Return the second largest number or -1 accordingly.

Example 1:
Input 1: 3
Input 2: {2,1,2}

Output: 1
Explanation:
1 is the second largest in the array.

Example 2:
Input 1: 5
Input 2: {4,7,9,8,0}

Output: 8
Explanation:
8 is the second largest in the array.

Solution in Python 3:

#https://codewindow.in
#join our telegram channel

l=int(input())
arr=list(map(int, input().split()))
arr=list(set(arr))
length_list=len(arr)

arr.sort()

#print("set = ",arr)
#print("length of set: ",length_list)

print(arr[length_list-2])

# Telegram @codewindow

Output:

5
4 7 9 8 0
8
3
2 1 2
1
Recent Posts

Leave a Comment

Your email address will not be published. Required fields are marked *