Print all sequences of given length | A utility function that prints a given arr of length size ; The core function that recursively generates and prints all sequences of length k ; A function that uses printSequencesRecur ( ) to prints all sequences from 1 , 1 , . .1 to n , n , . . n ; Driver Program to test above functions
function printArray ( arr , size ) { for ( i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; return ; } function printSequencesRecur ( arr , n , k , index ) { var i ; if ( k == 0 ) { printArray ( arr , index ) ; } if ( k > 0 ) { for ( i = 1 ; i <= n ; ++ i ) { arr [ index ] = i ; printSequencesRecur ( arr , n , k - 1 , index + 1 ) ; } } } function printSequences ( n , k ) { var arr = Array . from ( { length : k } , ( _ , i ) => 0 ) ; printSequencesRecur ( arr , n , k , 0 ) ; return ; } var n = 3 ; var k = 2 ; printSequences ( n , k ) ;