Longest subsequence with a given AND value | O ( N ) | C # implementation of the approach ; 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
using System ; using System . Collections . Generic ; class GFG { static int findLen ( int [ ] arr , int n , int m ) { List < int > filter = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & m ) == m ) filter . Add ( arr [ i ] ) ; if ( filter . Count == 0 ) return 0 ; int c_and = filter [ 0 ] ; for ( int i = 1 ; i < filter . Count ; i ++ ) c_and &= filter [ i ] ; if ( c_and == m ) return filter . Count ; return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 3 , 3 , 1 , 3 } ; int n = arr . Length ; int m = 3 ; Console . WriteLine ( findLen ( arr , n , m ) ) ; } }