Number of sub | Javascript implementation of th approach ; Function to return the number of sub - sequences divisible by 3 ; Base - cases ; If the state has been solved before then return its value ; Marking the state as solved ; Recurrence relation ; Driver code
var N = 100 var dp = Array . from ( Array ( N ) , ( ) => Array ( 3 ) ) ; var v = Array . from ( Array ( N ) , ( ) => Array ( 3 ) ) ; function findCnt ( s , i , r ) { if ( i == s . length ) { if ( r == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ r ] ) return dp [ i ] [ r ] ; v [ i ] [ r ] = 1 ; dp [ i ] [ r ] = findCnt ( s , i + 1 , ( r * 2 + ( s [ i ] - ' ' ) ) % 3 ) + findCnt ( s , i + 1 , r ) ; return dp [ i ] [ r ] ; } var s = " " ; document . write ( ( findCnt ( s , 0 , 0 ) - 1 ) ) ;