human
What is a code to check if elements of array can be arranged in AP , GP or HP and the other steps required given the following:
Check if elements of array can be arranged in AP , GP or HP | Returns true if arr [ 0. . n - 1 ] can form AP.
Base Case.
Sort array.
After sorting , difference between consecutive elements must be same ..
Traverse the given array and check if the difference between ith element and ( i - 1 ) th element is same or not.
Returns true if arr [ 0. . n - 1 ] can form GP.
Base Case.
Sort array.
After sorting , common ratio between consecutive elements must be same ..
Traverse the given array and check if the common ratio between ith element and ( i - 1 ) th element is same or not.
Returns true if arr [ 0. . n - 1 ] can form HP.
Base Case.
Find reciprocal of arr [ ].
After finding reciprocal , check if the reciprocal is in A . P . To check for A . P ..
Driver Code.
Function to check AP.
Function to check GP.
Function to check HP"
Solution in Python:
assistant
#Here is code to check if elements of array can be arranged in AP , GP or HP and the other steps required.
 def checkIsAP ( arr , n ) :
	if ( n == 1 ) :
		return True
	arr . sort ( ) ;
	d = arr [ 1 ] - arr [ 0 ]
	for i in range ( 2 , n ) :
		if ( arr [ i ] - arr [ i - 1 ] != d ) :
			return False
	return True
def checkIsGP ( arr , n ) :
	if ( n == 1 ) :
		return True
	arr . sort ( )
	r = arr [ 1 ] / arr [ 0 ]
	for i in range ( 2 , n ) :
		if ( arr [ i ] / arr [ i - 1 ] != r ) :
			return False
	return True
def checkIsHP ( arr , n ) :
	if ( n == 1 ) :
		return True
	rec = [ ]
	for i in range ( 0 , n ) :
		rec . append ( ( 1 / arr [ i ] ) )
	if ( checkIsAP ( rec , n ) ) :
		return True
	else :
		return False
arr = [ 1.0 / 5.0 , 1.0 / 10.0 , 1.0 / 15.0 , 1.0 / 20.0 ]
n = len ( arr )
flag = 0
if ( checkIsAP ( arr , n ) ) :
	print ( " Yes , ▁ An ▁ AP ▁ can ▁ be ▁ formed " , end =   ' ' )
	flag = 1
if ( checkIsGP ( arr , n ) ) :
	print ( " Yes , ▁ A ▁ GP ▁ can ▁ be ▁ formed " , end =   ' ' )
	flag = 1
if ( checkIsHP ( arr , n ) ) :
	print ( " Yes , ▁ A ▁ HP ▁ can ▁ be ▁ formed " , end =   ' ' )
	flag = 1
elif ( flag == 0 ) :
	print ( " No " , end =   ' ' )