forked from rjkalash/hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsetSum_mod_k.cpp
46 lines (33 loc) · 928 Bytes
/
subsetSum_mod_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
/*
Richa-15
Find if a subset exists such that subset sum mod k is zero
*/
#include<bits/stdc++.h>
using namespace std;
bool subModM(vector<int>& arr, int k){
map<int, int> ma;
ma.insert(make_pair(0,1));
int csum = 0, count = 0, rem;
for(auto x: arr){
csum += x;
rem = csum % k;
rem = (rem<0) ? k + rem : rem;
if(ma.find(rem) != ma.end())
//count += ma[rem]; //count no. of subsets
return true; // return when first subset is found
else
ma.insert(make_pair(rem,0));
ma[rem]++;
}
if(count){
cout<<"count:"<<count<<endl;
return true;
}
return false;
}
int main(){
vector<int> arr = {3, 1, 8, 7};
int k = 6;
subModM(arr, k) ? cout<<"subset found\n" : cout<<"no subset found\n";
return 0;
}