Skip to content

Instantly share code, notes, and snippets.

@aaronmaldonado-dev
Created May 20, 2021 20:52
Show Gist options
  • Save aaronmaldonado-dev/b2316a50e69f8898478f1cf766f4e157 to your computer and use it in GitHub Desktop.
Save aaronmaldonado-dev/b2316a50e69f8898478f1cf766f4e157 to your computer and use it in GitHub Desktop.
const getCombinations = (n) => {
const arr = [];
const maxValue = Math.pow(2, n);
for (let i = 0; i < maxValue; i++) {
arr.push(i.toString(2).padStart(n, '0'));
}
return arr.filter(item => {
const arrItem = item.split('');
for (let i = 0; i < n - 1; i++) {
const sum = parseInt(arrItem[i]) + parseInt(arrItem[i + 1]);
if (sum > 1) return false;
}
return true;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment