title: Maximum Sum Pairs Specific Difference
signature: func maximumSumPairsSpecificDifference(arr []int, n int, k int) int {
test_code: package main
  
  import (
      "fmt"
      "reflect"
      "math"
      "sort"
  )
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  func validateSolution(actual int, expected int) bool {
      return reflect.DeepEqual(actual,expected)
  }
  
  func driver(arr []int, n int, k int, expected int) string {
      if validateSolution(PLACEHOLDER_FN_NAME(arr, n, k),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver([]int{48, 53, 67, 78, 78, 93, 95}, 
  	6, 
  	4, 156)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver([]int{-70, -32, -2, 18, 30, 32, 52, 70, 72, 88}, 
  	8, 
  	8, 62)
      fmt.Printf("TEST-1...%s\n", result)
      
      result = driver([]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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}, 
  	37, 
  	31, 10)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver([]int{2, 3, 5, 5, 6, 10, 12, 13, 13, 16, 23, 23, 34, 36, 39, 40, 42, 45, 45, 47, 51, 51, 52, 56, 57, 58, 60, 60, 60, 60, 61, 63, 67, 68, 71, 73, 75, 80, 80, 82, 84, 86, 86, 86, 86, 89, 90, 98, 99}, 
  	31, 
  	37, 1083)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver([]int{-84, -56, 68, 78}, 
  	3, 
  	3, 0)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver([]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1}, 
  	14, 
  	9, 5)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver([]int{1, 2, 3, 9, 12, 12, 16, 17, 18, 19, 20, 21, 21, 26, 29, 42, 44, 45, 48, 48, 48, 54, 54, 55, 60, 63, 63, 64, 64, 67, 67, 68, 69, 74, 78, 78, 79, 83, 95, 95, 95, 96, 97, 99}, 
  	27, 
  	42, 849)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver([]int{-92, -58, -56, -48, -42, -16, -14, 28, 40, 42, 54, 60, 64}, 
  	9, 
  	8, 0)
      fmt.Printf("TEST-7...%s\n", result)
      
      result = driver([]int{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, 1}, 
  	21, 
  	19, 9)
      fmt.Printf("TEST-8...%s\n", result)
      
      result = driver([]int{25, 64, 96}, 
  	1, 
  	1, 0)
      fmt.Printf("TEST-9...%s\n", result)
      
          
  }
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 ];
  }