{{ message }}

Instantly share code, notes, and snippets.

# sebinsua/threeSum.js

Last active Nov 19, 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 const defaultKey = (arr) => arr.join(""); function createPermute(toKey = defaultKey) { return function* permute(arr, length = arr.length) { if (length > arr.length) { throw new Error( `The permutation size required (\${length}) is greater than the size of the array supplied (\${arr.length}).` ); } if (arr.length === 0) { return; } if (arr.length === 1) { yield [arr]; return; } if (arr.length === 2) { const [a, b] = arr; yield [a, b]; yield [b, a]; return; } let seen = new Set(); for (let idx = 0; idx < arr.length; idx++) { const first = arr[idx]; const tail = [...arr]; tail.splice(idx, 1); for (let part of permute(tail, length - 1)) { const permutation = [first, ...part].slice(0, length); const key = toKey(permutation); if (seen.has(key)) { continue; } seen.add(key); yield permutation; } } }; } function kSum(k) { const combinations = createPermute((arr) => [...arr].sort((a, b) => a - b).join("") ); const sum = (values) => values.reduce((a, b) => a + b, 0); return function* (arr, target = 0) { for (let values of combinations(arr, k)) { if (sum(values) === target) { yield values; } } }; } const threeSum = kSum(3); const fourSum = kSum(4); console.log("threeSum", Array.from(threeSum([-1, 0, 1]))); console.log("fourSum", Array.from(fourSum([-1, -1, 1, 1, 0, 0, -2, 2, -4, 6])));

### sebinsua commented Nov 16, 2020 • edited

This first version was based on my original permutations/combinations code.

I realise this is likely very inefficient and that the recursion is dangerous due to stack overflows however it's a working solution. I'll go back to the drawing board for the next version...!

### sebinsua commented Nov 19, 2020

```function* threeSum(_nums, target = 0) {
const nums = [..._nums].sort((a, b) => a - b);

let bdx = adx + 1;
let cdx = nums.length - 1;
while (bdx < cdx) {
const b = nums[bdx];
const c = nums[cdx];

const sum = a + b + c;

if (sum === target) {
yield [a, b, c];

while (bdx < cdx && b === nums[bdx + 1]) bdx++;
while (bdx < cdx && c === nums[cdx - 1]) cdx--;

bdx++;
cdx--;
} else if (sum < target) {
bdx++;
} /* if (sum > target) */ else {
cdx--;
}
}