-
Notifications
You must be signed in to change notification settings - Fork 0
/
Non-Divisible Subset
42 lines (39 loc) · 1.06 KB
/
Non-Divisible Subset
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
//https://www.hackerrank.com/challenges/non-divisible-subset/problem
#include <iostream>
using namespace std;
int main()
{
long int c;
int n,k,t,i,a[101]={0},s=0;
cin>>n>>k;
for(i=0;i<n;i++)
{
cin>>c;
t=c%k;
//we can directly store the remainders as
//were r intrested only in total elements
// and store its frequency in an array
a[t]++;
}
// if there there is 0, we can add it also, but not twice
//as (0+0)/k=0(evenly divides). hence we take it only once.
//NOTE: there is no k value as we are taking only remainder
if(a[0]>=1)
s=1;
for(i=1;i<=k/2;i++)
{
// we shouldn't have any 2 numbers a, b so that a+b=k
// so store only freq of either i or i-k , which is greater
// but for i=k/2 , we can take frequency as one only cos
// 2* (k/2)=k.
if(a[i]==a[k-i]&&a[i]>=1)
s+=1;
else if(a[i]>a[k-i])
s+=a[i];
else
s+=a[k-i];
//cout<<s<<" "<<i<<endl;
}
cout<<s;
return 0;
}