Space optimization using bit manipulations | index >> 5 corresponds to dividing index by 32 index & 31 corresponds to modulo operation of index by 32 Function to check value of bit position whether it is zero or one ; Sets value of bit for corresponding index ; Driver code ; Size that will be used is actual_size / 32 ceil is used to initialize the array with positive number ; Array is dynamically initialized as we are calculating size at run time ; Iterate through every index from a to b and call setbit ( ) if it is a multiple of 2 or 5
function checkbit ( array , index ) { return array [ index >> 5 ] & ( 1 << ( index & 31 ) ) ; } function setbit ( array , index ) { array [ index >> 5 ] |= ( 1 << ( index & 31 ) ) ; } let a = 2 , b = 10 ; let size = Math . abs ( b - a ) ; size = Math . ceil ( size / 32 ) ; let array = new Array ( size ) ; for ( let i = a ; i <= b ; i ++ ) if ( i % 2 == 0 i % 5 == 0 ) setbit ( array , i - a ) ; document . write ( " " ) ; for ( let i = a ; i <= b ; i ++ ) if ( checkbit ( array , i - a ) ) document . write ( i + " " ) ;