-
Notifications
You must be signed in to change notification settings - Fork 0
/
GFG Array Pair Sum Divisibility Problem
55 lines (48 loc) · 1.15 KB
/
GFG Array Pair Sum Divisibility Problem
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
class Solution {
public:
bool canPair(vector<int> nums, int k) {
int n=nums.size();
vector<int> rem(k,0);
for(int i=0;i<n;i++)
{
int r=nums[i]%k;
rem[r]++;
}
for(int i=0;i<k;i++)
{
if(rem[i]!=0)
{
if(i!=0)
{
if(rem[k-i]==0)
return false;
if(rem[i]<rem[k-i])
{
rem[i]=0;
rem[k-i]-=rem[i];
}
else
{
rem[k-i]=0;
rem[i]-=rem[k-i];
}
}
else
{
if(rem[i]%2==0)
{
rem[i]=0;
}
else
return false;
}
}
}
for(int i=0;i<k;i++)
{
if(rem[i]!=0)
return false;
}
return true;
}
};