title: What is the runtime and space complexity of this algorithm and why
body: <p>In practising some algorithms I came across the below problem, I'm having trouble figuring out what the time and space complexity is.</p>
  
  <p><strong>Problem:</strong>
  Print pairs of nums from array that add up to the sum of k.
  Eg</p>
  
  <pre><code>int[] arr =  new int[]{1, 7, 2, 3, 4};
  int k = 4;
  findSum(arr, k);
  </code></pre>
  
  <p>will output</p>
  
  <pre><code>Pair: 1, 3
  </code></pre>
  
  <p><strong>My Question:</strong>
  What is the runtime and space complexity of the solution below?</p>
  
  <p><strong>Java example below:</strong></p>
  
  <pre><code>private void findSum(int[] arr, int k) {
      if (arr == null || arr.length &lt; 2)
          throw new RuntimeException();
  
      Arrays.sort(arr);
      int i = 0; int j = arr.length -1;
      while (i &lt; j) {
          int sum = arr[i] + arr[j];
          if (sum == k)
          {
              System.out.println("Pair: " + arr[i] + ", " + arr[j]);
              i++;
          } else if (sum &gt; k) {
              j--;
          } else {
              i++;
          }
      }
  }
  </code></pre>