Longest subsequence with a given OR value : Dynamic Programming Approach | Javascript implementation of the approach ; To store the states of DP ; Function to return the required length ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
var maxN = 20 var maxM = 64 var dp = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) ) ; var v = Array . from ( Array ( maxN ) , ( ) => Array ( maxM ) ) ; function findLen ( arr , i , curr , n , m ) { if ( i == n ) { if ( curr == m ) return 0 ; else return - 1 ; } if ( v [ i ] [ curr ] ) return dp [ i ] [ curr ] ; v [ i ] [ curr ] = 1 ; var l = findLen ( arr , i + 1 , curr , n , m ) ; var 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 ] ; } var arr = [ 3 , 7 , 2 , 3 ] ; var n = arr . length ; var m = 3 ; var ans = findLen ( arr , 0 , 0 , n , m ) ; if ( ans == - 1 ) document . write ( 0 ) ; else document . write ( ans ) ;