Bitwise AND of N binary strings | Function to find the bitwise AND of all the binary strings ; To store the largest and the smallest string ' s ▁ size , ▁ We ▁ need ▁ this ▁ to ▁ add ▁ ▁ ' 0 's in the resultant string ; Reverse each string Since we need to perform AND operation on bits from Right to Left ; Update the respective length values ; Traverse bits from 0 to smallest string 's size ; If at this bit position , there is a 0 in any of the given strings then AND operation on current bit position will be 0 ; Add resultant bit to result ; Add 0 's to the string. ; Reverse the string Since we started from LEFT to RIGHT ; Return the resultant string ; Utility reverse function ; Swap values of left and right ; Driver code
function strBitwiseAND ( arr , n ) { let res = " " ; let smallest_size = Number . MAX_VALUE ; let largest_size = Number . MIN_VALUE ; let temp = " " ; for ( let i = 0 ; i < n ; i ++ ) { temp += arr [ i ] ; arr [ i ] = reverse ( temp ) ; smallest_size = Math . min ( smallest_size , arr [ i ] . length ) ; largest_size = Math . max ( largest_size , arr [ i ] . length ) ; } for ( let i = 0 ; i < smallest_size ; i ++ ) { let all_ones = true ; for ( let j = 0 ; j < n ; j ++ ) { if ( arr [ j ] [ i ] == ' ' ) { all_ones = false ; break ; } } res += ( all_ones ? ' ' : ' ' ) ; } for ( let i = 0 ; i < largest_size - smallest_size ; i ++ ) res += ' ' ; let temp1 = " " ; temp1 += res ; res = reverse ( temp1 ) ; let temparray1 = res ; let Temparray = " " ; for ( let i = 6 ; i < temparray1 . length ; i ++ ) { Temparray = Temparray + temparray1 [ i ] ; } return Temparray ; } function reverse ( input ) { let temparray = input . split ( ' ' ) ; let left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { let temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return temparray . join ( " " ) ; } let arr = [ " " , " " , " " ] ; let n = arr . length ; document . write ( strBitwiseAND ( arr , n ) ) ;