Number of subsets with same AND , OR and XOR values in an Array | Javascript implementation to find the number of subsets with equal bitwise AND , OR and XOR values ; Function to find the number of subsets with equal bitwise AND , OR and XOR values ; Traverse through all the subsets ; Finding the subsets with the bits of ' i ' which are set ; Computing the bitwise AND ; Computing the bitwise OR ; Computing the bitwise XOR ; Comparing all the three values ; Driver Code
let mod = 1000000007 ; function countSubsets ( a , n ) { let answer = 0 ; for ( let i = 0 ; i < ( 1 << n ) ; i ++ ) { let bitwiseAND = - 1 ; let bitwiseOR = 0 ; let bitwiseXOR = 0 ; for ( let j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) { if ( bitwiseAND == - 1 ) bitwiseAND = a [ j ] ; else bitwiseAND &= a [ j ] ; bitwiseOR |= a [ j ] ; bitwiseXOR ^= a [ j ] ; } } if ( bitwiseAND == bitwiseOR && bitwiseOR == bitwiseXOR ) answer = ( answer + 1 ) % mod ; } return answer ; } let N = 6 ; let A = [ 1 , 3 , 2 , 1 , 2 , 1 ] ; document . write ( countSubsets ( A , N ) ) ;