Subarray / Substring vs Subsequence and Programs to Generate them | Javascript code to generate all possible subsequences . Time Complexity O ( n * 2 ^ n ) ; Number of subsequences is ( 2 * * n - 1 ) ; Run from counter 000. . 1 to 111. . 1 ; Check if jth bit in the counter is set If set then print jth element from arr [ ] ; Driver Code
function printSubsequences ( arr , n ) { let opsize = parseInt ( Math . pow ( 2 , n ) , 10 ) ; for ( let counter = 1 ; counter < opsize ; counter ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( ( counter & ( 1 << j ) ) != 0 ) document . write ( arr [ j ] + " " ) ; } document . write ( " " ) ; } } let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; document . write ( " " + " " ) ; printSubsequences ( arr , n ) ;