Pairwise - Trouble with specific test case (or spec)

Pairwise - Trouble with specific test case (or spec)
0

#1

Hi there – In this challenge I’m wondering if the problem spec and one of the test cases are in conflict. I’m having problems specifically with this test case:
pairwise([0, 0, 0, 0, 1, 1], 1)
My code outputs “4” for this test, but the directions say it should be “10”, which I assume is because it is adding the index pairs (0,4) and (1,5) for 0+4+1+5 = 10. (Indices 0 and 4 correspond to element values 0 and 1, which add up to 1, while indices 1 and 5 correspond to element values 0 and 1, which add up to 1.)

However, the directions say “If multiple pairs are possible that have the same numeric elements but different indices, return the smallest sum of indices.” For that test case, the index pair (0,4) has element values of 0 and 1, while the index pair (1,5) also has element values of 0 and 1. Those are the same numeric elements, right? So shouldn’t the program only output the smallest sum of 0+4 while ignoring 1+5?

I don’t understand how 10 is the correct output, unless the spec really means to say: “If multiple pairs are possible that have the same numeric elements and also one shared index, then return the smallest sum of indices; but otherwise if the numeric elements are the same and both index pairs are different, then add both index pairs.” All of that is really oddly specific, so I don’t think that was the intention. Any help understanding this?

Thanks for the guidance. I’m pretty sure my code is more complicated than it has to be to do this regardless of that test case, but any tips are appreciated!

Your code so far

function pairwise(arr, arg) {
  var out;
  // find element pairs in arr that sum to arg
  var pairelems = [];
  var pairindices = [];
  var pairsum = [];
  // search through arr for pairs; don't reuse indices
  for(var i=0; i<arr.length; i++){
    for(var j=i+1; j<arr.length; j++){
      if(arr[i]+arr[j]===arg){
        // save numeric pairs; put smallest first for easy compare later
        var small; 
        var large;
        if(arr[i]<arr[j]){
          small = arr[i];
          large = arr[j];
        }
        else{
          small = arr[j];
          large = arr[i];
        }
        pairelems.push([small,large]);
        pairindices.push([i,j]);
        // save sum of indices of pairs
        pairsum = pairsum.concat(i+j);
      }
    }
  }

  // if any repeat pairs, note lowest sum of indices
  var indexRemove = [];
  for(var k=0; k<pairelems.length; k++){
    for(var l=k+1; l<pairelems.length; l++){
      // check if any repeat element pairs
      if(pairelems[k][0]===pairelems[l][0] && 
         pairelems[k][1]===pairelems[l][1]){
        // save indices of repeats if not already saved once
        if(indexRemove.indexOf(k)<0){
          indexRemove.push(k);          
        }
        if(indexRemove.indexOf(l)<0){
          indexRemove.push(l);        
        }
      } 
    }
  }
  //remove indices denoted EXCEPT the one for the smallest pairsum
  var ordered = pairsum;
  ordered.sort(function(a,b){
    return a-b;
  });
  indexRemove.splice(pairsum.indexOf(ordered[0]),1);
  for(var m=0; m<indexRemove.length; m++){
    pairsum[indexRemove[m]]=0; // set to zero so it won't add up
  }
  // output sum of all sums of indices  
  out = pairsum.reduce(function(a,b){
    return a+b;
  });                                  
  //out = pairelems;  //debug
  // out = pairsum; //debug
  return out;
}

//pairwise([1,4,2,3,0,5], 7);
//pairwise([1, 1, 1], 2);
pairwise([0, 0, 0, 0, 1, 1], 1);```
**Your browser information:**

Your Browser User Agent is: ```Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36```.

**Link to the challenge:**
https://www.freecodecamp.org/challenges/pairwise

#2

The intent of the following challenge instruction

If multiple pairs are possible that have the same numeric elements but different indices, return the smallest sum of indices.

is return the sum of the indices (for all the pairs that sum to the 2nd argument) which is the smallest total of the indices involved.