


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’.
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.
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
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.
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 have to check this one case.
If the remainder of the first element of a pair is ‘X’ then the remainder of the second element of the pair should be ‘K’ - ‘X’. Because then only (‘firstEle’ + ‘secondEle’) % ‘K’ = 0.
Let’s assume the first element is ‘A’ and the second element is ‘B’.
If A is divided by K and the remainder is X:
A % K = X i.e A = n * K + X
If B is divide by K and the remainder is K - X:
B % K = K - X i.e B = m * K + K - X
Then, add these two equations:
A + B = n * K + m * K + K + X - X => A + B = (m + n + 1) * K => (A + B) % K = 0.
So basically first we have to store the remainder of all numbers in a HashMap ‘freq’ when dividing each number by ‘K’. Then check the frequency of the remainder of a number i.e freq of ‘X’ is equal to the freq of ‘K’ - ‘X’ i.e freq[‘X’] = freq[‘K’ - ‘X’].
And we have to handle some edge cases also: