title: Maximum Sum Pairs Specific Difference
signature: maximum_sum_pairs_specific_difference <- 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(48L, 53L, 67L, 78L, 78L, 93L, 95L), 6L, 4L, 
          156L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-70L, -32L, -2L, 18L, 30L, 32L, 52L, 70L, 72L, 88L), 8L, 8L, 
          62L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 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), 37L, 31L, 
          10L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(2L, 3L, 5L, 5L, 6L, 10L, 12L, 13L, 13L, 16L, 23L, 23L, 34L, 36L, 39L, 40L, 42L, 45L, 45L, 47L, 51L, 51L, 52L, 56L, 57L, 58L, 60L, 60L, 60L, 60L, 61L, 63L, 67L, 68L, 71L, 73L, 75L, 80L, 80L, 82L, 84L, 86L, 86L, 86L, 86L, 89L, 90L, 98L, 99L), 31L, 37L, 
          1083L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-84L, -56L, 68L, 78L), 3L, 3L, 
          0L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 14L, 9L, 
          5L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(1L, 2L, 3L, 9L, 12L, 12L, 16L, 17L, 18L, 19L, 20L, 21L, 21L, 26L, 29L, 42L, 44L, 45L, 48L, 48L, 48L, 54L, 54L, 55L, 60L, 63L, 63L, 64L, 64L, 67L, 67L, 68L, 69L, 74L, 78L, 78L, 79L, 83L, 95L, 95L, 95L, 96L, 97L, 99L), 27L, 42L, 
          849L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-92L, -58L, -56L, -48L, -42L, -16L, -14L, 28L, 40L, 42L, 54L, 60L, 64L), 9L, 8L, 
          0L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(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, 1L), 21L, 19L, 
          9L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(25L, 64L, 96L), 1L, 1L, 
          0L
      )))
  
  }
  
  main()
source_py: def maximum_sum_pairs_specific_difference(arr, N, K):
      arr.sort()
      dp = ([0] * N)
      dp[0] = 0
      for i in range(1, N):
          dp[i] = dp[(i - 1)]
          if ((arr[i] - arr[(i - 1)]) < K):
              if (i >= 2):
                  dp[i] = max(dp[i], ((dp[(i - 2)] + arr[i]) + arr[(i - 1)]))
              else:
                  dp[i] = max(dp[i], (arr[i] + arr[(i - 1)]))
      return dp[(N - 1)]
source_cpp: int maximum_sum_pairs_specific_difference(vector<int> arr, int N, int K) {
    sort(arr.begin(), arr.end());
    int dp [ N ];
    dp [ 0 ] = 0;
    for ( int i = 1;
    i < N;
    i ++ ) {
      dp [ i ] = dp [ i - 1 ];
      if ( arr [ i ] - arr [ i - 1 ] < K ) {
        if ( i >= 2 ) dp [ i ] = max ( dp [ i ], dp [ i - 2 ] + arr [ i ] + arr [ i - 1 ] );
        else dp [ i ] = max ( dp [ i ], arr [ i ] + arr [ i - 1 ] );
      }
    }
    return dp [ N - 1 ];
  }