Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions solutions/19.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Find pairs that sum to x
// Return array of pairs from an input array of numbers that adds up to second input x. Each number can be used only once.
// input: [1,2,3,4,5], 6
// output: [[1,5], [2,4]]

// Solution by Zach Nagatani @zachnagatani

/**
* Returns an multidimensional array containing pairs of numbers from an passed in array which add up to the passed in sum
* Each integer can only be used to form one pair
* @param {number[]} arr - An array of numbers to search through
* @param {number} sum - The sum to use as the basis of the search
* @returns {number[][]} pairs - A multidimensional array of pairs that add up to the sum
*/
const solution = (arr, sum) => {
let unusedNums = arr.slice(),
pairs = [],
head = 0,
tail = unusedNums.length - 1;

while (head < tail) {
let currentSum = unusedNums[head] + unusedNums[tail];

if (currentSum === sum) {
pairs.push([unusedNums[head], unusedNums[tail]]);
unusedNums.splice(head, 1);
unusedNums.splice(tail, 1);

head = 0;
tail = unusedNums.length - 1;
} else if (currentSum < sum) {
head++;
} else {
tail--;
}
}

return pairs.length ? pairs : null;
};

module.exports = {
solution
};
23 changes: 23 additions & 0 deletions test/19.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const expect = require('chai').expect;
let solution = require('../solutions/19').solution;
//solution = require('../yourSolution').solution;

describe('Array pairs that sum to x', () => {
it('should return an array of pairs of integers whose sum equal to x', () => {
const result = solution([1,2,3,4,5,6], 6);
expect(result).to.deep.equal([[1,5], [2,4]]);

const result2 = solution([2,1,3,4,5,6], 6);
expect(result).to.deep.equal([[1,5], [2,4]]);
});

it('should only use an integer once in its output', () => {
const result = solution([1,3,3,3], 4);
expect(result).to.deep.equal([[1,3]]);
});

it('should return null of no pairs are found', () => {
const result = solution([1,2,3,4,5,6], 40);
expect(result).to.equal(null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return [] makes more sense to represent an empty set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Also, I've realized my solution only works on a sorted array. Is that a reasonable constraint, or should I rework it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I was going to point that something was off. We can't assume its a sorted array. Real life scenario: We get a list of test scores and we want to pair people up so that the teams are even. We can't assume the list of test scores are sorted.

});
});