Shortest & Second Shortest Distance

Shortest & Second Shortest Distance - TCS DIGITAL

Problem: 

There are N paths to travel from the city A to the city B. The distance of each path is passed as the input to the program. The program must print the shortest distance and the second shortest distance among the given N distances as the output. If all the distances are equal, then the program must print the string value "Equal" as the output. 

Boundary Condition(s): 

2 <= N <= 100 

1 <= Distance of each path <= 10^8 

Input Format: 

The first line contains N. The next N lines, each contains an integer value representing the distance of each path from the city A to the city B. 

Output Format: 

The first line contains the shortest distance and the second shortest distance among the given N distances separated by a space or the first line contains a string value "Equal". 

Example Input/Output 1: 

Input: 

400 

100 

500 

200 

300 

Output: 

100 200 

Explanation: Here N=5, the distance of each path from the city A to the city B are given below. 400 100 500 200 300 The distance 100 is the shortest distance and 200 is the second shortest distance. So 100 200 is printed. 

Example Input/Output 2: 

Input: 

45 

20 

65 

20 

Output: 

20 45 

Example Input/Output 3: 

Input: 

75 

75 

75 

75 

75 

75 

Output: 

Equal

Program:

n = int(input()) 

l = []

for i in range(n): 

    l.append(int(input()))

l = sorted(set(l))[::-1] 

if len(l) == 1:

    print("Equal")

else:    

    print(l[-1],l[-2])    

Comments

Popular posts from this blog

Pronic Integers in N - InfyTQ question

Count Strong Points - Accenture

Letters at position of multiples of a number