title: Count Pairs Difference Equal K 1
signature: class Solution {
  
      public int CountPairsDifferenceEqualK1(List<int> arr, int n, int k) {
test_code: using System;
  using System.Numerics;
  using System.Diagnostics;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Security.Cryptography;
  using System.Web.UI;
  using System.Web.Script.Serialization;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  namespace EvaluatePrediction {
      class TestRunner {
          public bool ValidateSolution(int actual, int expected){
              return new JavaScriptSerializer().Serialize(actual) == new JavaScriptSerializer().Serialize(expected);
          }
  
          public string driver(List<int> arr, int n, int k, int expected){
              try{
                PLACEHOLDER_CLS_NAME solution_cls = new PLACEHOLDER_CLS_NAME();
                int pred_result = solution_cls.PLACEHOLDER_FN_NAME(arr, n, k); 
                if (ValidateSolution(
                      pred_result,
                      expected)){
                  return "PASSED";
                }
                return "FAILED";
              } 
              catch (Exception exception_obj)
              {
                return exception_obj.GetType().FullName;
              }
          }
          static void Main(string[] args) {
              TestRunner runner = new TestRunner();
              String result;
              result = runner.driver(
                  new List<int>{5, 5, 10, 19, 29, 32, 40, 60, 65, 70, 72, 89, 92}, 7, 12, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-0...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{-98, -90, -88, -82, -80, -78, -74, -74, -72, -62, -58, -58, -56, -52, -48, -46, -38, -38, -30, -12, -10, 4, 8, 8, 14, 22, 24, 26, 36, 40, 40, 48, 56, 58, 58, 58, 60, 62, 64, 68, 68, 74, 84}, 24, 36, 
                  7
              );
              Console.WriteLine(string.Format(
                  "TEST-1...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{0, 0, 1}, 1, 1, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-2...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{1, 7, 13, 14, 16, 29, 34, 42, 44, 49, 59, 62, 65, 66, 70, 71, 73, 73, 76, 80, 96, 96}, 12, 16, 
                  1
              );
              Console.WriteLine(string.Format(
                  "TEST-3...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{-98, -88, -58, -56, -48, -34, -22, -18, -14, -14, -8, -4, -2, 2, 18, 38, 42, 46, 54, 68, 70, 90, 94, 96, 98}, 23, 22, 
                  4
              );
              Console.WriteLine(string.Format(
                  "TEST-4...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{0, 1, 1}, 2, 1, 
                  1
              );
              Console.WriteLine(string.Format(
                  "TEST-5...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{11, 43, 50, 58, 60, 68, 75}, 4, 4, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-6...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{-80, -56, -10, 0, 6, 8, 24, 42, 52, 86, 88, 94}, 11, 9, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-7...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 29, 30, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-8...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{4, 7, 9, 9, 14, 34, 34, 36, 50, 54, 80, 81, 99}, 9, 8, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-9...{0}",
                  result)
              );
          }
      }
  }
  
source_py: def count_pairs_difference_equal_k_1(arr, n, k):
      count = 0
      arr.sort()
      l = 0
      r = 0
      while (r < n):
          if ((arr[r] - arr[l]) == k):
              count += 1
              l += 1
              r += 1
          elif ((arr[r] - arr[l]) > k):
              l += 1
          else:
              r += 1
      return count
source_cpp: int count_pairs_difference_equal_k_1(vector<int> arr, int n, int k) {
    int count = 0;
    sort(arr.begin(), arr.end());
    int l = 0;
    int r = 0;
    while ( r < n ) {
      if ( arr [ r ] - arr [ l ] == k ) {
        count ++;
        l ++;
        r ++;
      }
      else if ( arr [ r ] - arr [ l ] > k ) l ++;
      else r ++;
    }
    return count;
  }