Find the occurrence of the given binary pattern in the binary representation of the array elements | Function to return the binary representation of n ; Array to store binary representation ; Counter for binary array ; Storing remainder in binary array ; To store the binary representation as a string ; Function to return the frequency of pat in the given string txt ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; If pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Function to find the occurrence of the given pattern in the binary representation of elements of arr [ ] ; For every element of the array ; Find its binary representation ; Print the occurrence of the given pattern in its binary representation ; Driver code
function decToBinary ( n ) { var binaryNum = Array ( 32 ) ; var i = 0 ; while ( n > 0 ) { binaryNum [ i ] = n % 2 ; n = parseInt ( n / 2 ) ; i ++ ; } var binary = " " ; for ( var j = i - 1 ; j >= 0 ; j -- ) binary += ( binaryNum [ j ] . toString ( ) ) ; return binary ; } function countFreq ( pat , txt ) { var M = pat . length ; var N = txt . length ; var res = 0 ; for ( var i = 0 ; i <= N - M ; i ++ ) { var j ; for ( j = 0 ; j < M ; j ++ ) if ( txt [ i + j ] != pat [ j ] ) break ; if ( j == M ) { res ++ ; j = 0 ; } } return res ; } function findOccurrence ( arr , n , pattern ) { for ( var i = 0 ; i < n ; i ++ ) { var binary = decToBinary ( arr [ i ] ) ; document . write ( countFreq ( pattern , binary ) + " " ) ; } } var arr = [ 5 , 106 , 7 , 8 ] ; var pattern = " " ; var n = arr . length ; findOccurrence ( arr , n , pattern ) ;