


Ninja has been given an array/list ‘ARR’ of even size ‘N’ and an integer ‘K’. Ninja gives his friend a task to divide the given ‘ARR’ into ‘N’/2 pairs such that the sum of each pair should be divisible by ‘K’.
Can you help Ninja to do this task?
For Example :
If ‘ARR’ = [4 5 6 5 7 3] and ‘K’ = 5
Pairs are(4, 6), (5, 5) ,and (7, 3) and the sum of elements of each pair is divisible by ‘K’ i.e 5.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain two single space-separated integers ‘N’ and ‘K’ which represent the size of ‘ARR’ and an integer from which the sum of each pair can be divisible.
The next line contains ‘N’ space-separated integers representing the values of the ‘ARR’.
Output Format :
For each test case, print "true" if Ninja’s friend can divide the ‘ARR’ into two parts according to the above condition else print "false" without quotes.
Output for every test case will be printed in a separate line.
Note :
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 50
2 <= ‘N’ <= 10000
1 <= ‘K’ <= 100000
-100000 <= ‘ARR[i]’ <= 100000
Where ‘ARR’[i] represents the value of the ‘ARR’ array/list.
Time limit: 1 sec
2
2 5
-15 15
4 20
10 20 30 40
true
true
In the first test case, there is only 1 pair i.e (-15, 15) and the sum of this pair is 0. 0 is divisible by 5.
In the second test case, pairs are (10, 30) and (20, 40) and the sum of elements of each pair is divisible by ‘K’ i.e 20.
2
6 2
2 3 4 5 9 7
4 7
10 6 20 4
true
false
In the first test case, pairs are (2, 4), (3, 7), and (5, 9) and the sum of elements of each pair is divisible by ‘K’ i.e 2.
In the second test case, there is no way to make pairs whose sum is divisible by 7.
Can you think to solve this by iterating over all combinations of pairs?
As we know we have to check if the sum of all ‘ARR’ pairs are divisible by a number ‘K’ or not. To check the sum of elements of a pair is divisible by ‘K’ or not, we are going to add the remainder of each pair and if the remainder is 0 or ‘K’ that means we get the pair.
The steps are as follows:
O(N ^ 2), where ‘N’ is the size of ‘ARR’.
For finding all possible pairs we are iterating over ‘ARR’ using a nested loop. Hence the time complexity is O(N ^ 2).
O(N), where ‘N’ is the size of ‘ARR’.
Because if we find a pair that satisfies the condition which is mentioned above. Then we are adding the indexes of elements involve in this pair into the HashSet ‘vis’. Hence, the overall space complexity is O(N).