Count L | Fumction to find the number of arrays of length L such that each element divides the next element ; Stores the number of sequences of length i that ends with j ; Base Case ; Iterate over the range [ 0 , l ] ; Iterate for all multiples of j ; Incrementing dp [ i + 1 ] [ k ] by dp [ i ] [ j ] as the next element is multiple of j ; Stores the number of arrays ; Add all array of length L that ends with i ; Return the resultant count ; Driver Code
function numberOfArrays ( n , l ) { let dp = Array ( l + 1 ) . fill ( ) . map ( ( ) => Array ( n + 1 ) . fill ( 0 ) ) ; dp [ 0 ] [ 1 ] = 1 ; for ( let i = 0 ; i < l ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { for ( let k = j ; k <= n ; k += j ) { dp [ i + 1 ] [ k ] += dp [ i ] [ j ] ; } } } let ans = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { ans += dp [ l ] [ i ] ; } return ans ; } let N = 2 , L = 4 ; document . write ( numberOfArrays ( N , L ) ) ;