Skip to content

Commit

Permalink
first implementation of 2-sum
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleygu committed Mar 18, 2013
1 parent 834aca7 commit 44f699f
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions HW6.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ ints.forEach(function(element, index, array) {
ints[index] = parseInt(element, 10);
});


// generate hash table
var intHash = {};
ints.forEach(function(element, index, array) {
if (intHash[element] === null) { // element not in array
intHash[element] = 1;
} else {
intHash[element] += 1;
}
});

var countSums = function(a, b, hash) { // a and b are range of t's
var sums = 0;
for (var t = a; t <= b; t++) {
for (var x in hash) {
var numX = hash[x];
var numY = hash[t-hash[x]];
sums += Math.min(numX, numY);
}
}
return sums;
};

console.log(countSums(2500, 4000, intHash));

/*
Question 2
Download the text file here.
Expand Down

0 comments on commit 44f699f

Please sign in to comment.