


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).