# Longest subsequence with a given AND value

• Last Updated : 08 Mar, 2022

Given an array arr[] and an integer M, the task is to find the longest subsequence with a given AND value M. If there is no such sub-sequence then print 0
Examples:

Input: arr[] = {3, 7, 2, 3}, M = 3
Output:
Longest sub-sequence with AND value 3 is {3, 7, 3}.
Input: arr[] = {2, 2}, M = 3
Output:

Approach: A simple way to solve this will be to generate all the possible sub-sequences and then find the largest among them with required AND value.
However, for smaller values of M, an approach based on dynamic programming can be used.
Let’s look at the recurrence relation first.

dp[i][curr_and] = max(dp[i + 1][curr_and], dp[i + 1][curr_and & arr[i]] + 1)

Let’s understand the states of DP now. Here, dp[i][curr_and] stores the longest subsequence of subarray arr[i…N-1] such that curr_and & the AND of this subsequence is equal to M. At each step, either the index i can be chosen updating curr_and or it can be rejected.
Below is the implementation of the above approach:

## C++

 // C++ implementation of the approach#include using namespace std;#define maxN 20#define maxM 64 // To store the states of DPint dp[maxN][maxM];bool v[maxN][maxM]; // Function to return the required lengthint findLen(int* arr, int i, int curr,            int n, int m){    // Base case    if (i == n) {        if (curr == m)            return 0;        else            return -1;    }     // If the state has been solved before    // return the value of the state    if (v[i][curr])        return dp[i][curr];     // Setting the state as solved    v[i][curr] = 1;     // Recurrence relation    int l = findLen(arr, i + 1, curr, n, m);    int r = findLen(arr, i + 1, curr & arr[i],                    n, m);    dp[i][curr] = l;    if (r != -1)        dp[i][curr] = max(dp[i][curr], r + 1);    return dp[i][curr];} // Driver codeint main(){    int arr[] = { 3, 7, 2, 3 };    int n = sizeof(arr) / sizeof(int);    int m = 3;     int ans = findLen(arr, 0, ((1 << 8) - 1),                      n, m);    if (ans == -1)        cout << 0;    else        cout << ans;     return 0;}

## Java

 // Java implementation of the approachimport java.util.*; class GFG{static int maxN = 300;static int maxM = 300; // To store the states of DPstatic int dp[][] = new int[maxN][maxM];static boolean v[][] = new boolean[maxN][maxM]; // Function to return the required lengthstatic int findLen(int[] arr, int i,                   int curr, int n, int m){    // Base case    if (i == n)    {        if (curr == m)            return 0;        else            return -1;    }     // If the state has been solved before    // return the value of the state    if (v[i][curr])        return dp[i][curr];     // Setting the state as solved    v[i][curr] = true;     // Recurrence relation    int l = findLen(arr, i + 1, curr, n, m);    int r = findLen(arr, i + 1, curr & arr[i], n, m);    dp[i][curr] = l;    if (r != -1)        dp[i][curr] = Math.max(dp[i][curr], r + 1);    return dp[i][curr];} // Driver codepublic static void main(String args[]){    int arr[] = { 3, 7, 2, 3 };    int n = arr.length;    int m = 3;     int ans = findLen(arr, 0, ((1 << 8) - 1), n, m);    if (ans == -1)        System.out.print( 0);    else        System.out.print( ans);}} // This code is contributed by Arnab Kundu

## Python3

 # Python3 implementation of the approachimport numpy as np maxN = 20maxM = 256 # To store the states of DPdp = np.zeros((maxN, maxM));v = np.zeros((maxN, maxM)); # Function to return the required lengthdef findLen(arr, i, curr, n, m) :     # Base case    if (i == n) :        if (curr == m) :            return 0;        else :            return -1;     # If the state has been solved before    # return the value of the state    if (v[i][curr]) :        return dp[i][curr];     # Setting the state as solved    v[i][curr] = 1;     # Recurrence relation    l = findLen(arr, i + 1, curr, n, m);    r = findLen(arr, i + 1, curr & arr[i], n, m);         dp[i][curr] = l;         if (r != -1) :        dp[i][curr] = max(dp[i][curr], r + 1);             return dp[i][curr]; # Driver codeif __name__ == "__main__" :     arr = [ 3, 7, 2, 3 ];    n = len(arr);    m = 3;     ans = findLen(arr, 0, ((1 << 8) - 1), n, m);         if (ans == -1) :        print(0);    else :        print(ans); # This code is contributed by AnkitRai01

## C#

 // C# implementation of the approachusing System; class GFG{static int maxN = 300;static int maxM = 300; // To store the states of DPstatic int[,] dp = new int[maxN, maxM];static bool[,] v = new bool[maxN, maxM]; // Function to return the required lengthstatic int findLen(int[] arr, int i,                   int curr, int n, int m){    // Base case    if (i == n)    {        if (curr == m)            return 0;        else            return -1;    }     // If the state has been solved before    // return the value of the state    if (v[i, curr])        return dp[i, curr];     // Setting the state as solved    v[i, curr] = true;     // Recurrence relation    int l = findLen(arr, i + 1, curr, n, m);    int r = findLen(arr, i + 1, curr & arr[i], n, m);    dp[i, curr] = l;    if (r != -1)        dp[i, curr] = Math.Max(dp[i, curr], r + 1);    return dp[i, curr];} // Driver codepublic static void Main(String[] args){    int[] arr = { 3, 7, 2, 3 };    int n = arr.Length;    int m = 3;     int ans = findLen(arr, 0, ((1 << 8) - 1), n, m);    if (ans == -1)        Console.WriteLine(0);    else        Console.WriteLine(ans);}} // This code is contributed by// sanjeev2552

## Javascript


Output:
3

Time Complexity: O(N * maxVal) where maxVal is the maximum element from the given array.

Auxiliary Space: O(maxN * maxM)

My Personal Notes arrow_drop_up