subArray.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function printSubsequences(arr, index, subarr) | |
{ | |
// Print the subsequence when reach | |
// the leaf of recursion tree | |
if (index === arr.length) | |
{ | |
// Condition to avoid printing | |
// empty subsequence | |
if (subarr.length != 0) | |
console.log(subarr); | |
} | |
else | |
{ | |
// Subsequence without including | |
// the element at current index | |
printSubsequences(arr, index + 1, subarr); | |
subarr = subarr.concat([arr[index]]); | |
// Subsequence including the element | |
// at current index | |
printSubsequences(arr, index + 1, subarr); | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment