Skip to content

Instantly share code, notes, and snippets.

@ddallala
Created February 3, 2018 01:18
Show Gist options
  • Select an option

  • Save ddallala/b80d54f5e1d85a56d888dbee8eee181a to your computer and use it in GitHub Desktop.

Select an option

Save ddallala/b80d54f5e1d85a56d888dbee8eee181a to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/duxobek
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// https://www.youtube.com/watch?v=XKu_SEDAykw
// find pairs of numbers that equal a given sum
function findPairsForSum(a,sum){
if(a.length <= 1) return [];
//if(a[0]>sum) return [];
var pairs = [];
for(var i=1;i<a.length;i++){
if(a[0]+a[i] == sum) pairs.push([a[0],a[i]]);
}
var anext_pairs = findPairsForSum(a.slice(1),sum);
if(anext_pairs.length>0) pairs = pairs.concat(anext_pairs);
return pairs;
}
console.log(findPairsForSum([1,2,7,6],8));
function findPairsForSumHash(a,sum){
var hash = {};
var pairs = [];
for(var i=0;i<a.length;i++){
// found the opposite
if(hash[a[i]] !== undefined ) pairs.push([sum-a[i], a[i]])
// not found, add the opposite to the hash
else hash[sum-a[i]] = true;
}
return pairs;
}
console.log(findPairsForSumHash([1,2,4,4],8));
</script>
<script id="jsbin-source-javascript" type="text/javascript">// https://www.youtube.com/watch?v=XKu_SEDAykw
// find pairs of numbers that equal a given sum
function findPairsForSum(a,sum){
if(a.length <= 1) return [];
//if(a[0]>sum) return [];
var pairs = [];
for(var i=1;i<a.length;i++){
if(a[0]+a[i] == sum) pairs.push([a[0],a[i]]);
}
var anext_pairs = findPairsForSum(a.slice(1),sum);
if(anext_pairs.length>0) pairs = pairs.concat(anext_pairs);
return pairs;
}
console.log(findPairsForSum([1,2,7,6],8));
function findPairsForSumHash(a,sum){
var hash = {};
var pairs = [];
for(var i=0;i<a.length;i++){
// found the opposite
if(hash[a[i]] !== undefined ) pairs.push([sum-a[i], a[i]])
// not found, add the opposite to the hash
else hash[sum-a[i]] = true;
}
return pairs;
}
console.log(findPairsForSumHash([1,2,4,4],8));</script></body>
</html>
// https://www.youtube.com/watch?v=XKu_SEDAykw
// find pairs of numbers that equal a given sum
function findPairsForSum(a,sum){
if(a.length <= 1) return [];
//if(a[0]>sum) return [];
var pairs = [];
for(var i=1;i<a.length;i++){
if(a[0]+a[i] == sum) pairs.push([a[0],a[i]]);
}
var anext_pairs = findPairsForSum(a.slice(1),sum);
if(anext_pairs.length>0) pairs = pairs.concat(anext_pairs);
return pairs;
}
console.log(findPairsForSum([1,2,7,6],8));
function findPairsForSumHash(a,sum){
var hash = {};
var pairs = [];
for(var i=0;i<a.length;i++){
// found the opposite
if(hash[a[i]] !== undefined ) pairs.push([sum-a[i], a[i]])
// not found, add the opposite to the hash
else hash[sum-a[i]] = true;
}
return pairs;
}
console.log(findPairsForSumHash([1,2,4,4],8));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment