title: Count Pairs Difference Equal K 1
signature: count_pairs_difference_equal_k_1 <- function(arr, n, k) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, k, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n, k), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(5L, 5L, 10L, 19L, 29L, 32L, 40L, 60L, 65L, 70L, 72L, 89L, 92L), 7L, 12L, 
          0L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-98L, -90L, -88L, -82L, -80L, -78L, -74L, -74L, -72L, -62L, -58L, -58L, -56L, -52L, -48L, -46L, -38L, -38L, -30L, -12L, -10L, 4L, 8L, 8L, 14L, 22L, 24L, 26L, 36L, 40L, 40L, 48L, 56L, 58L, 58L, 58L, 60L, 62L, 64L, 68L, 68L, 74L, 84L), 24L, 36L, 
          7L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 1L), 1L, 1L, 
          0L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(1L, 7L, 13L, 14L, 16L, 29L, 34L, 42L, 44L, 49L, 59L, 62L, 65L, 66L, 70L, 71L, 73L, 73L, 76L, 80L, 96L, 96L), 12L, 16L, 
          1L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-98L, -88L, -58L, -56L, -48L, -34L, -22L, -18L, -14L, -14L, -8L, -4L, -2L, 2L, 18L, 38L, 42L, 46L, 54L, 68L, 70L, 90L, 94L, 96L, 98L), 23L, 22L, 
          4L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 1L, 1L), 2L, 1L, 
          1L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(11L, 43L, 50L, 58L, 60L, 68L, 75L), 4L, 4L, 
          0L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-80L, -56L, -10L, 0L, 6L, 8L, 24L, 42L, 52L, 86L, 88L, 94L), 11L, 9L, 
          0L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 29L, 30L, 
          0L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(4L, 7L, 9L, 9L, 14L, 34L, 34L, 36L, 50L, 54L, 80L, 81L, 99L), 9L, 8L, 
          0L
      )))
  
  }
  
  main()
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;
  }