Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6380 |

@mb1973 | 5642 |

@Quandray | 5233 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4610 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3413 |

Complete Leaderboard | |

Handle | Score |

@sanskar94511 | 603 |

@akashverma4 | 363 |

@swastik8750 | 300 |

@nishunishuraj18 | 286 |

@surajmamgai | 264 |

@anks9013 | 222 |

@nayan2709 | 213 |

@burhanuddinbohra72 | 212 |

@realname | 203 |

@amank8975 | 203 |

@manmeetkaur18102000 | 202 |

Complete Leaderboard |

Given an array of integers **arr[]** and a number **K**.You can pair two numbers of the array if the difference between them is strictly less than **K**. The task is to find the maximum possible sum of such disjoint pairs. The Sum of **P** pairs is the sum of all **2P** numbers of pairs.

**Example 1:**

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

**Example 2:**

**Input : **
arr[] = {5, 15, 10, 300}
K = 12
**Output : **
25

**Your Task: **

You don't need to read input or print anything. Your task is to complete the function **maxSumPairWithDifferenceLessThanK()** which takes the array **arr[]**, its size **N**** **and an integer **K **as inputs and returns the maximum possible sum of disjoint pairs.

**Expected Time Complexity:** O(N. log(N))

**Expected Auxiliary Space: **O(N)

**Constraints:**

1 ≤ N ≤ 10^{5}

0 ≤ K ≤ 10^{5}

1 ≤ arr[i] ≤ 10^{4}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Pairs with specific difference

...