Skip to content

Instantly share code, notes, and snippets.

@leckylao
Last active Aug 1, 2020
Embed
What would you like to do?
subArray.js
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