Tuesday, July 4, 2017

Min-Max -Practice Problem from Hackerearth and solution using python 2.7.6

Problem Statement:

Given an array of integers . Check if all the numbers between minimum and maximum number in array exist's within the array .
Print 'YES' if numbers exist otherwise print 'NO'(without quotes).
Input:
Integer N denoting size of array
Next line contains N space separated integers denoting elements in array
Output:
Output your answer
Constraints:
1<= N <= 1000
1<= a[i] <= 100

Programming using python 2.7.6:


The problem statement is pretty straight forward and can be written in many different ways. However I am using set to solve this :

Code in Python 2.7:


N = int(raw_input())
a = map(int,raw_input().split())
b = [x for x in range(min(a),max(a)+1,1)]
if list(set(b).difference(a)) == []: print "YES"
else: print "NO"

1 comment: