-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubSetSumDivisibleByM.cpp
More file actions
59 lines (57 loc) · 1.35 KB
/
SubSetSumDivisibleByM.cpp
File metadata and controls
59 lines (57 loc) · 1.35 KB
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
#include<iostream>
using namespace std;
//Time Complexity is O(m^2) And Space Complexity is O(m)
bool sssdm(int *ar,int n,int m)
{
if(n>m)
{
return true;
}
bool *dp=new bool[m];
for(int i=0;i<m;i++)
{
dp[m]=false;
}
for(int i=0;i<n;i++)
{
if (!dp[0]) {
bool *temp = new bool[m];
for (int j = 0; j < m; j++) {
temp[j] = false;
}
for (int j = 0; j < m; j++) {
if (!dp[j]) continue;
if (dp[(j + ar[i]) % m]) continue;
temp[(j + ar[i]) % m] = true;
}
for (int j = 0; j < m; j++) {
if (!temp[j]) continue;
dp[j] = true;
}
dp[ar[i] % m] = true;
} else {
return true;
}
}
return dp[0];
}
int main()
{
int n;
cout<<"Enter the value of n:\n";
cin>>n;
int *ar=new int[n];
cout<<"Enter the value of array:\n";
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
int m;
cout<<"Enter the value of m:\n";
cin>>m;
if (!sssdm(ar, n, m)) {
cout << "Subset with modulo = " << m << "does not exists." << endl;
} else {
cout << "Subset with modulo = " << m << " exists." << endl;
}
}