title: Pairs algorithm on HackerRank
body: <p>Let me quickly explain the problem</p>
  
  <p>originally from here: <a href="https://www.hackerrank.com/challenges/pairs" rel="nofollow noreferrer">https://www.hackerrank.com/challenges/pairs</a></p>
  
  <p><strong>Sample input is:</strong></p>
  
  <pre><code>5 2  
  1 5 3 4 2
  </code></pre>
  
  <p><strong>referencing the above input</strong></p>
  
  <p><em>first line:</em></p>
  
  <pre><code>N = 5, K = 2 
  </code></pre>
  
  <p><em>where N is the number of integers in the set, and K is the difference we are looking for</em></p>
  
  <p><em>second line (as an array):</em></p>
  
  <pre><code>[1, 5, 3, 4, 2];
  </code></pre>
  
  <p><em>How many pairs in the array have a difference of K ?</em></p>
  
  <p><em>sample answer:</em>
  <strong>"There are 3 pairs of integers in the set with a difference of 2."</strong></p>
  
  <p><em>here is something I'm working on for this, but I am missing something :</em></p>
  
  <pre><code>function findDifference(k, nArr) {
  
      nArr.sort((a, b) =&gt; a - b);
  
  var pairsWithDifferenceOfK = 0;
  
  for (var i = 0; i &lt; nArr.length; i++) {
      if (Math.abs((nArr[i] - ((nArr.length &gt; (i + k)) ? nArr[(i + k)] : nArr[i]))) === k) {
         pairsWithDifferenceOfK++;
     };
  }
     console.log(pairsWithDifferenceOfK);
  }
  var K = 2;
  Arr = [1, 5, 3, 4, 2];
  findDifference(K, Arr);
  </code></pre>
  
  <p><strong>output:</strong></p>
  
  <pre><code>3
  </code></pre>
  
  <p>This fails when given these inputs ( and I am curious as to why this fails on some inputs and not others):</p>
  
  <pre><code>10 1
  363374326 364147530 61825163 1073065718 1281246024 1399469912 42804763 491595254 879792181 1069262793
  </code></pre>