-
Notifications
You must be signed in to change notification settings - Fork 0
/
q15_GS_array_pair_sum_divisibility_problem.cpp
57 lines (54 loc) · 1.4 KB
/
q15_GS_array_pair_sum_divisibility_problem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*Given an array of integers and a number k, write a function that returns true if given array can be divided into pairs
such that sum of every pair is divisible by k.*/
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
bool canPair(vector<int> nums, int k) {
// Code here.
map<int, vector<int>> maps;
int i,flag = 0;
if(nums.size() % 2 != 0){
return false;
}
for(i = 0; i < nums.size(); i++){
maps[nums[i] % k].push_back(nums[i]);
}
if(maps[0].size() % 2 == 0){
flag = 1;
}
for(i = 1; i < k/2 + 1; i++){
if(maps[i].size() == maps[k-i].size() && i != k-i){
flag = 1;
}
else if(i == k-i && maps[i].size() % 2 == 0){
flag = 1;
}
else{
flag = 0;
break;
}
}
return flag;
}
};
// { Driver Code Starts.
int main() {
int tc;
cin >> tc;
while (tc--) {
int n, k;
cin >> n >> k;
vector<int> nums(n);
for (int i = 0; i < nums.size(); i++) cin >> nums[i];
Solution ob;
bool ans = ob.canPair(nums, k);
if (ans)
cout << "True\n";
else
cout << "False\n";
}
return 0;
} // } Driver Code Ends