function solve(arr) { let nums = arr[0].split(' ').map(x => Number(x)); // 1 answer for the empty set let ans = 1; // nums.length answers for picking each element by itself if (nums.length >= 1) { ans += nums.length; } // calculate all arithmetic progressions made by picking 2 out of N, 3 out of N... N out of N // all 2 out of N progressions are always valid and can technically be extracted into a separate nChooseK calculation // but with progressions of 3 or more elements, we have to check if they are actually valid arithmetic progressions // i.e. 10 20 30 is but 10 20 45 is not if (nums.length > 1) { for (let i = 2; i <= nums.length; i++) { for (let startIndex = 0; startIndex < nums.length - (i - 1); startIndex++) { recursivelyFindProgressions(i, startIndex, nums, 1, undefined); } } } console.log(ans); function recursivelyFindProgressions(length, curIndex, nums, curLength, step) { for (let j = curIndex + 1; j < nums.length; j++) { let num = nums[j]; let lastNum = nums[curIndex]; if(curLength == 1) { step = num - lastNum; } if (step === (num - lastNum)) { if(curLength + 1 === length) { ans++; } else { recursivelyFindProgressions(length, j, nums, curLength + 1, step); } } } } }