You are viewing a single comment's thread. Return to all comments →
There is better - O(n) solution:
let dictArr = {};
for (let i = 0; i < arr.length; i++) { dictArr[arr[i]] = dictArr[arr[i]] ? dictArr[arr[i]] + 1 : 1; } let counter = 0; for (let i = 0; i < arr.length; i++) { let hasToBe = arr[i] - k; if (dictArr[hasToBe]) counter += dictArr[hasToBe] } return counter;
Seems like cookies are disabled on this browser, please enable them to open this website
Pairs
You are viewing a single comment's thread. Return to all comments →
There is better - O(n) solution:
let dictArr = {};