Skip to content

Instantly share code, notes, and snippets.

@leckylao
Last active August 1, 2020 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leckylao/fee42ee54deda756d6f52d7bd6164093 to your computer and use it in GitHub Desktop.
Save leckylao/fee42ee54deda756d6f52d7bd6164093 to your computer and use it in GitHub Desktop.
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