Check if all array elements can be reduced to less than X | Function to check if all array elements can be reduced to less than X or not ; Checks if all array elements are already a X or not ; Traverse every possible pair ; Calculate GCD of two array elements ; If gcd is a 1 ; If gcd is a X , then a pair is present to reduce all array elements to a X ; If no pair is present with gcd is a X ; Function to check if all array elements area X ; Function to calculate gcd of two numbers ; Driver Code
function findAns ( A , N , X ) { if ( check ( A , X ) ) { return true ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { let gcdd = gcd ( A [ i ] , A [ j ] ) ; if ( gcdd != 1 ) { if ( gcdd <= X ) { return true ; } } } } return false ; } function check ( A , X ) { for ( let i = 0 ; i < A . length ; i ++ ) { if ( A [ i ] > X ) { return false ; } } return true ; } function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } let X = 4 ; let A = [ 2 , 1 , 5 , 3 , 6 ] ; let N = 5 ; document . write ( findAns ( A , N , X ) ) ;