Count numbers from a given range that are not divisible by any of the array elements | Function to find the non - multiples till k ; Stores all unique multiples ; Iterate the array ; For finding duplicates only once ; Inserting all multiples into the set ; Returning only the count of numbers that are not divisible by any of the array elements ; Function to count the total values in the range [ L , R ] ; Count all values in the range using exclusion principle ; Driver Code ; Function Call
function findNonMultiples ( arr , n , k ) { let multiples = new Set ( ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( ! multiples . has ( arr [ i ] ) ) { for ( let j = 1 ; j <= k / arr [ i ] ; j ++ ) { multiples . add ( arr [ i ] * j ) ; } } } return k - multiples . size ; } function countValues ( arr , N , L , R ) { return findNonMultiples ( arr , N , R ) - findNonMultiples ( arr , N , L - 1 ) ; } let arr = [ 2 , 3 , 4 , 5 , 6 ] ; let N = arr . length ; let L = 1 , R = 20 ; document . write ( countValues ( arr , N , L , R ) ) ;