Maximize count of distinct elements possible in an Array from the given operation | Function to find the gcd of two numbers ; Function to calculate and return the count of maximum possible distinct elements in the array ; Find the maximum element ; Base Case ; Finding the gcd of first two element ; Calculate Gcd of the array ; Return the total count of distinct elements ; Driver Code
def gcd ( x , y ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT return y NEW_LINE DEDENT return gcd ( y % x , x ) NEW_LINE DEDENT def findDistinct ( arr , n ) : NEW_LINE INDENT maximum = max ( arr ) NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n == 2 ) : NEW_LINE INDENT return ( maximum // gcd ( arr [ 0 ] , arr [ 1 ] ) ) NEW_LINE DEDENT k = gcd ( arr [ 0 ] , arr [ 1 ] ) NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT k = gcd ( k , arr [ i ] ) NEW_LINE DEDENT return ( maximum // k ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findDistinct ( arr , n ) ) NEW_LINE DEDENT