Online Node.js Compiler

const arr = [1, 3, 7, 11, 14, 25, 39]; const longestFibonacci = (arr = []) => { const map = arr.reduce((acc, num, index) => { acc[num] = index return acc }, {}) const memo = arr.map(() => arr.map(() => 0)) let max = 0 for(let i = 0; i < arr.length; i++) { for(let j = i + 1; j < arr.length; j++) { const a = arr[i] const b = arr[j] const index = map[b - a] if(index < i) { memo[i][j] = memo[index][i] + 1 } max = Math.max(max, memo[i][j]) } } return max > 0 ? max + 2 : 0 }; console.log(longestFibonacci(arr));