-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-if-an-array-can-be-divided-into-pairs-whose-sum-is-divisible-by-k.cpp
66 lines (64 loc) · 1.64 KB
/
check-if-an-array-can-be-divided-into-pairs-whose-sum-is-divisible-by-k.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
58
59
60
61
62
63
64
65
66
//
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ft first
#define sc second
#define pb push_back
#define ins insert
#define all(v) v.begin(), v.end()
#define Sort(v) sort(all(v))
#define s(a, n) sort(a, a + n)
#define f1(i,a,b) for(ll i=a;i<b;i++)
#define f2(i,a,b,c) for(ll i=a;i<b;i+=c)
#define f3(i,a,b) for(ll i=a;i>=b;i--)
#define f4(i,a,b,c) for(ll i=a;i>=b;i-=c)
#define jatin(t) int t; cin >> t; while(t--)
const int N = 1e5+2, MOD = 1e9+7;
//int array[N];
//cout << (condition ? "YES\n" : "NO\n");
// { 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.
unordered_map<int,int> mp;
if(nums.size()%2!=0){
return false;
}
for(int i=0;i<nums.size();i++){
mp[nums[i]%k]++;
}
if(mp[0]%2==1){
return false;
}
unordered_map<int,int> :: iterator it;
for(it=mp.begin();it!=mp.end();it++){
if(it->second!=mp[(k-it->first)%k]){
return false;
}
}
return true;
}
};
// { 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