human
Check whether Arithmetic Progression can be formed from the given array | Returns true if a permutation of arr [ 0. . n - 1 ] can form arithmetic progression.
Find the smallest and and update second smallest.
Find second smallest.
Check if the duplicate element found or not.
If duplicate found then return false.
Find the difference between smallest and second smallest.
As we have used smallest and second smallest , so we should now only check for n - 2 elements.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to check whether Arithmetic Progression can be formed from the given array and the other steps required.
 def checkIsAP ( arr , n ) :
	hm = { }
	smallest = float ( ' inf ' )
	second_smallest = float ( ' inf ' )
	for i in range ( n ) :
		if ( arr [ i ] < smallest ) :
			second_smallest = smallest
			smallest = arr [ i ]
		elif ( arr [ i ] != smallest and arr [ i ] < second_smallest ) :
			second_smallest = arr [ i ]
		if arr [ i ] not in hm :
			hm [ arr [ i ] ] = 1
		else :
			return False
	diff = second_smallest - smallest
	for i in range ( n - 1 ) :
		if ( second_smallest ) not in hm :
			return False
		second_smallest += diff
	return True
arr = [ 20 , 15 , 5 , 0 , 10 ]
n = len ( arr )
if ( checkIsAP ( arr , n ) ) :
	print ( " Yes " )
else :
	print ( " No " )