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
def checkIsAP ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT arr . sort ( ) ; NEW_LINE d = arr [ 1 ] - arr [ 0 ] NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( arr [ i ] - arr [ i - 1 ] != d ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def checkIsGP ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT arr . sort ( ) NEW_LINE r = arr [ 1 ] / arr [ 0 ] NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( arr [ i ] / arr [ i - 1 ] != r ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def checkIsHP ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT rec = [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT rec . append ( ( 1 / arr [ i ] ) ) NEW_LINE DEDENT if ( checkIsAP ( rec , n ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT arr = [ 1.0 / 5.0 , 1.0 / 10.0 , 1.0 / 15.0 , 1.0 / 20.0 ] NEW_LINE n = len ( arr ) NEW_LINE flag = 0 NEW_LINE if ( checkIsAP ( arr , n ) ) : NEW_LINE INDENT print ( " Yes , ▁ An ▁ AP ▁ can ▁ be ▁ formed " , end =   ' ' ) NEW_LINE flag = 1 NEW_LINE DEDENT if ( checkIsGP ( arr , n ) ) : NEW_LINE INDENT print ( " Yes , ▁ A ▁ GP ▁ can ▁ be ▁ formed " , end =   ' ' ) NEW_LINE flag = 1 NEW_LINE DEDENT if ( checkIsHP ( arr , n ) ) : NEW_LINE INDENT print ( " Yes , ▁ A ▁ HP ▁ can ▁ be ▁ formed " , end =   ' ' ) NEW_LINE flag = 1 NEW_LINE DEDENT elif ( flag == 0 ) : NEW_LINE INDENT print ( " No " , end =   ' ' ) NEW_LINE DEDENT