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.

Vagesh Verma | 166 |

vamsikrishna55 | 162 |

UrGuru | 150 |

abducodes | 145 |

adithya_99 | 119 |

abducodes | 701 |

harshtyagi36 | 326 |

sandipbhaumik | 291 |

Akhil varma | 267 |

arpit_anshuman | 262 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.