Longest subsequence with a given AND value | O ( N ) | Function to return the required length ; To store the filtered numbers ; Filtering the numbers ; If there are no elements to check ; Find the AND of all the filtered elements ; Check if the AND is equal to m ; Driver code
function findLen ( arr , n , m ) { var filter = [ ] ; for ( var i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & m ) == m ) filter . push ( arr [ i ] ) ; if ( filter . length == 0 ) return 0 ; var c_and = filter [ 0 ] ; for ( var i = 1 ; i < filter . length ; i ++ ) c_and &= filter [ i ] ; if ( c_and == m ) return filter . length ; return 0 ; } var arr = [ 7 , 3 , 3 , 1 , 3 ] ; var n = arr . length ; var m = 3 ; document . write ( findLen ( arr , n , m ) ) ;