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
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function findPossibleDerivables ( $ arr , $ n , $ D , $ A , $ B ) { $ gcdAB = gcd ( $ A , $ B ) ; $ counter = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ( abs ( $ arr [ $ i ] - $ D ) % $ gcdAB ) == 0 ) { $ counter ++ ; } } return $ counter ; } $ arr = array ( 1 , 2 , 3 , 4 , 7 , 13 ) ; $ n = sizeof ( $ arr ) ; $ D = 5 ; $ A = 4 ; $ B = 2 ; echo findPossibleDerivables ( $ arr , $ n , $ D , $ A , $ B ) , " " ; $ a = array ( 1 , 2 , 3 ) ; $ n = sizeof ( $ a ) ; $ D = 6 ; $ A = 3 ; $ B = 2 ; echo findPossibleDerivables ( $ arr , $ n , $ D , $ A , $ B ) , " " ; ? >