Count of triplets ( a , b , c ) in the Array such that a divides b and b divides c | Function to count triplets ; Iterate for middle element ; Iterate left array for a [ i ] ; Iterate right array for a [ k ] ; Return the final result ; Driver code
def getCount ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( 1 , n - 1 ) : NEW_LINE INDENT p , q = 0 , 0 NEW_LINE for i in range ( j ) : NEW_LINE INDENT if ( arr [ j ] % arr [ i ] == 0 ) : NEW_LINE INDENT p += 1 NEW_LINE DEDENT DEDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT if ( arr [ k ] % arr [ j ] == 0 ) : NEW_LINE INDENT q += 1 NEW_LINE DEDENT DEDENT count += p * q NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( getCount ( arr , N ) ) NEW_LINE DEDENT