Skip to content

Instantly share code, notes, and snippets.

@timlesallen
Last active September 16, 2020 23:00
Show Gist options
  • Save timlesallen/4e11306a535960418251aa9770647f7a to your computer and use it in GitHub Desktop.
Save timlesallen/4e11306a535960418251aa9770647f7a to your computer and use it in GitHub Desktop.
function fibonacciLike (input) {
const sequences = [];
input.forEach((number, i) => {
// Append to the all sequences where this is a legitimate continuation.
// We need to consider all of them because any values of a, b where a + b = c could form
// part of the solution depending on what comes afterwards.
sequences.forEach(sequence => {
const [a, b] = sequence;
if (a + b === number) sequence.unshift(number);
});
// Need to add this as a combo with every other number so far.
// We shouldn't be doubling up because the input in increasing monotonically.
for (let j = 0; j < i; j++) {
sequences.unshift([number, input[j]]);
}
});
const max = Math.max(...sequences.map(sequence => sequence.length));
return max >= 3 ? max : 0;
}
module.exports = fibonacciLike;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment