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
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function findPossibleDerivables ( arr , n , D , A , B ) { var gcdAB = gcd ( A , B ) ; var counter = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( Math . abs ( arr [ i ] - D ) % gcdAB ) == 0 ) { counter ++ ; } } return counter ; } var arr = [ 1 , 2 , 3 , 4 , 7 , 13 ] ; var n = arr . length ; var D = 5 , A = 4 , B = 2 ; document . write ( findPossibleDerivables ( arr , n , D , A , B ) + " " ) ; var a = [ 1 , 2 , 3 ] ; n = a . length ; D = 6 ; A = 3 ; B = 2 ; document . write ( findPossibleDerivables ( a , n , D , A , B ) ) ;