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

Examples:

```
Input : A[] = {3, 5, 10, 15, 17, 12, 9}, K = 4
Output : 62
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.
Input : A[] = {5, 15, 10, 300}, k = 12
Output : 25
```

**Input:**

The first line of input contains an integer T denoting the no of test cases . Then T test cases follow. Each test case contains 3 lines . The first line of each test case is an integer N denoting the size of the array A. In the next line are N space separated values of the array A. In the next line is an integer x.

**Output:**

For each test case output in a new line the maximum possible sum of disjoint pairs

**Constraints:**

1<=T<100

1<=N<=100

1<=A[] <=1000

**Example:
Input:**

2

7

3 5 10 15 17 12 9

4

4

5 15 10 300

12

62

25

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

agness | 122 |

aman1931998 | 102 |

Survivor | 100 |

uchitnitk | 96 |

lonecoder | 95 |

KshatriyaYash | 1066 |

nikhil_sojan | 1032 |

lonecoder | 1030 |

mazumderrohit8 | 899 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4492 |

Quandray | 4444 |

Login to report an issue on this page.