Skip to content

Instantly share code, notes, and snippets.

@khalid32
Created June 1, 2017 04:58
Show Gist options
  • Save khalid32/e158b928ee90a078a35b89e3f6c61aae to your computer and use it in GitHub Desktop.
Save khalid32/e158b928ee90a078a35b89e3f6c61aae to your computer and use it in GitHub Desktop.
Freecodecamp's Advanced Algorithm Scripting Challenge
/*
Given an array arr, find element pairs whose sum equal the second argument arg and return the sum of their indices.
If multiple pairs are possible that have the same numeric elements but different indices, return the smallest sum of indices. Once an element has been used, it cannot be reused to pair with another.
For example pairwise([7, 9, 11, 13, 15], 20) returns 6. The pairs that sum to 20 are [7, 13] and [9, 11]. We can then write out the array with their indices and values.
Index 0 1 2 3 4
Value 7 9 11 13 15
Below we'll take their corresponding indices and add them.
7 + 13 = 20 → Indices 0 + 3 = 3
9 + 11 = 20 → Indices 1 + 2 = 3
3 + 3 = 6 → Return 6
*/
function pairwise(arr, arg) {
var total = 0, checkIndex = [];
for(var i = 0; i < arr.length; i++){
for(var j = i+1; j < arr.length; j++){
if(arr[i] + arr[j] === arg && checkIndex.indexOf(i) === -1 && checkIndex.indexOf(j) === -1){
checkIndex.push(i); checkIndex.push(j);
total += i+j;
}
}
}
return total;
}
pairwise([1,4,2,3,0,5], 7);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment