VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-sum-pairs-specific-difference/

⇱ Sum of Pairs with Diff < k - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sum of Pairs with Diff < k

Last Updated : 1 Jun, 2026

Given an array arr[] and an integer k, group the numbers into pairs to get maximum possible total sum of the pairs.

To form a valid pair, the following conditions must hold:

  • The absolute difference between the two numbers in the pair must be strictly less than k.
  • Pairs should be disjoint (each number from the array can only be used in a maximum of one pair).

Return the maximum possible total sum of the valid pairs.

Examples:

Input  : arr[] = [3, 5, 10, 15, 17, 12, 9], k = 4
Output : 62
Explanation: The disjoint pairs with difference less than k are, (3, 5), (10, 12), (15, 17), therefore maximum sum which we can get is 3 + 5 + 12 + 10 + 15 + 17 = 62. Note that an alternate way to form disjoint pairs is, (3, 5), (9, 12), (15, 17), but this pairing produces lesser sum.

Input  : arr[] = [5, 15, 10, 300], k = 12
Output : 25

[Naive Approach] Using Recursion – O(2^n) Time and O(n) Space

The idea is to try all possible ways of forming disjoint pairs using recursion. For each unused element, we either pair it with another valid element (whose difference is less than k) or skip it. We explore every possible combination and keep track of the maximum sum obtained. Since we are exploring all subsets of pairings, this guarantees the optimal answer but is highly inefficient.

  • Sort the array and maintain a visited array to track used elements
  • For each unused element, try pairing it with all valid unused elements and recurse
  • Also consider the option of skipping the current element and recurse
  • Track and return the maximum sum from all recursive possibilities

Output
62

[Better Approach] Using Dynamic Programming + Sorting – O(n log n) Time and O(n) Space

The idea is to first sort the array so that elements close in value come together, making it easier to form valid pairs with difference less than k. Then, we use dynamic programming where at each index we decide whether to include the current element in a pair with its previous element or skip it. This ensures that we form disjoint pairs while maximizing the total sum.

  • Sort the array to bring closer elements together for valid pairing
  • Use a DP array where each index stores the maximum sum possible till that point
  • At each index, either skip the element or form a pair with the previous element if difference < k
  • Take the maximum of both choices and return the last DP value

Output
62

[Expected Approach] Using Greedy – O(n log n) Time and O(1) Space

The idea is to maximize the sum by always picking the largest possible valid pair first. After sorting the array, we iterate from the end so that bigger elements are considered first. If two consecutive elements have a difference less than k, we pair them and add their sum. Since larger numbers contribute more to the total, this greedy choice ensures a maximum sum while keeping pairs disjoint.

  • Sort the array in ascending order
  • Traverse from the end to prioritize larger elements
  • If the difference between current and previous element is less than k, add both to sum and skip them
  • Continue until all elements are processed and return the total sum

Dry Run Example:

  • Initial array: [3, 5, 10, 15, 17, 12, 9], k = 4
  • Sorted array: [3, 5, 9, 10, 12, 15, 17], sum = 0
  • i = 6 : Compare 17 and 15 (Diff: 2 < 4) → Valid! add 32 (sum = 32), skip 15, move i to 4.
  • i = 4 : Compare 12 and 10 (Diff: 2 < 4) → Valid! add 22 (sum = 54), skip 10, move i to 2.
  • i = 2 : Compare 9 and 5 (Diff: 4 is not < 4) → Invalid, add nothing, move i to 1.
  • i = 1 : Compare 5 and 3 (Diff: 2 < 4) → Valid! add 8 (sum = 62), skip 3, loop ends.
    Final Maximum Sum: 62

Output
62
Comment
Article Tags: