Number of elements from the array which are reachable after performing given operations on D | Function to return the GCD of a and b ; Function to return the count of reachable integers from the given array ; GCD of A and B ; To store the count of reachable integers ; If current element can be reached ; Return the count ; Driver code
function GCD ( a , b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } function findReachable ( arr , D , A , B , n ) { var gcd_AB = GCD ( A , B ) ; var count = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - D ) % gcd_AB == 0 ) count ++ ; } return count ; } var arr = [ 4 , 5 , 6 , 7 , 8 , 9 ] ; var n = arr . length ; var D = 4 , A = 4 , B = 6 ; document . write ( findReachable ( arr , D , A , B , n ) ) ;