Last active
August 1, 2020 05:41
-
-
Save leckylao/fee42ee54deda756d6f52d7bd6164093 to your computer and use it in GitHub Desktop.
subArray.js
This file contains hidden or 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