title: Optimizing this C# algorithm (K Difference)
body: <p>This is the problem I'm solving (it's a sample problem, not a real problem):</p>
  
  <blockquote>
    <p>Given N numbers , [N&lt;=10^5] we need to count the total pairs of
    numbers that have a difference of K. [K>0 and K&lt;1e9]</p>
    
    <p>Input Format: 1st line contains N &amp; K (integers). 2nd line contains N
    numbers of the set. All the N numbers are assured to be distinct.
    Output Format: One integer saying the no of pairs of numbers that have
    a diff K.</p>
  </blockquote>
  
  <pre><code>Sample Input #00:
  5 2
  1 5 3 4 2
  Sample Output #00:
  3
  Sample Input #01:
  10 1
  363374326 364147530 61825163 1073065718 1281246024 1399469912 428047635 491595254 879792181 1069262793 
  Sample Output #01:
  0
  </code></pre>
  
  <p>I already have a solution (and I haven't been able to optimize it as well as I had hoped). Currently my solution gets a score of <strong>12/15</strong> when it is run, and I'm wondering why I can't get 15/15 (my solution to another problem wasn't nearly as efficient, but got all of the points). Apparently, the code is run using "Mono 2.10.1, C# 4".</p>
  
  <p><strong>So can anyone think of a better way to optimize this further?</strong> The VS profiler says to avoid calling String.Split and Int32.Parse. The calls to Int32.Parse can't be avoided, although I guess I could optimize tokenizing the array.</p>
  
  <p>My current solution:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Linq;
  
  namespace KDifference
  {
     class Solution
     {
        static void Main(string[] args)
        {
           char[] space = { ' ' };
  
           string[] NK = Console.ReadLine().Split(space);
           int N = Int32.Parse(NK[0]), K = Int32.Parse(NK[1]);
  
           int[] nums = Console.ReadLine().Split(space, N).Select(x =&gt; Int32.Parse(x)).OrderBy(x =&gt; x).ToArray();
  
           int KHits = 0;
  
           for (int i = nums.Length - 1, j, k; i &gt;= 1; i--)
           {
              for (j = 0; j &lt; i; j++)
              {
                 k = nums[i] - nums[j];
  
                 if (k == K)
                 {
                    KHits++;
                 }
                 else if (k &lt; K)
                 {
                    break;
                 }
              }
           }
  
           Console.Write(KHits);
        }
     }
  }
  </code></pre>