You are viewing a single comment's thread. Return to all comments →
I think test cases are incorrect
JS:
function pickingNumbers(a) { let longestSubarrayLen = 0 let i = 0; while (i < a.length) { let arr = [a[i]]; const visitedIndexes = []; let j = i+1; while (j < a.length) { const current = a[j]; const last = arr[arr.length-1] const prev = arr.length > 1 ? arr[arr.length - 2] : undefined; if (Math.abs(current - last) <=1 && !visitedIndexes.includes(j)) { arr.push(a[j]); visitedIndexes.push(j); if (longestSubarrayLen < arr.length) { longestSubarrayLen = arr.length console.log({arr}) } } else if (prev && j === a.length - 1) { j = visitedIndexes[visitedIndexes.length - 2] || i+1; arr.pop(); } j++ } i++; } return longestSubarrayLen; }
Seems like cookies are disabled on this browser, please enable them to open this website
Picking Numbers
You are viewing a single comment's thread. Return to all comments →
I think test cases are incorrect
JS: