Number of array elements derivable from D after performing certain operations | Function to return gcd of a and b ; Function to Return the number of elements of arr [ ] which can be derived from D by performing ( + A , - A , + B , - B ) ; find the gcd of A and B ; counter stores the number of array elements which can be derived from D ; arr [ i ] can be derived from D only if | arr [ i ] - D | is divisible by gcd of A and B ; Driver Code
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) ; NEW_LINE DEDENT def findPossibleDerivables ( arr , n , D , A , B ) : NEW_LINE INDENT gcdAB = gcd ( A , B ) NEW_LINE counter = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( abs ( arr [ i ] - D ) % gcdAB ) == 0 ) : NEW_LINE INDENT counter += 1 NEW_LINE DEDENT DEDENT return counter NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 7 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE D , A , B = 5 , 4 , 2 NEW_LINE print ( findPossibleDerivables ( arr , n , D , A , B ) ) NEW_LINE a = [ 1 , 2 , 3 ] NEW_LINE n = len ( a ) NEW_LINE D , A , B = 6 , 3 , 2 NEW_LINE print ( findPossibleDerivables ( a , n , D , A , B ) ) NEW_LINE DEDENT