Join Regular Classroom : Visit ClassroomTech

Revature Question Solve | Smallest and the 3rd smallest element | Codewindow.in

Write a program to find the difference between the smallest and the 3rd smallest element in the
array.
The resultant output should be a positive integer

Input Format
First line contains an integer, denotes the size of the array
Second line contains the elements of an array.

Sample Input
5
2 3 4 5 6
Sample Output
2

Explanation
The smallest element in the array is 2 and the third smallest element in the array is 4. Hence, 4-2
= 2.
All the test cases will have more than 4 elements.

Solution:
Python 3.7:

#Solution by codewindow.in
x=int(input())
n=list(map(int, input().split()))
n.sort()

res = abs(n[0]-n[2])
print(res)

Also Checkout

Recent Posts
Pages