-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvenSubsetSumProblem.cpp
More file actions
91 lines (87 loc) · 2.23 KB
/
EvenSubsetSumProblem.cpp
File metadata and controls
91 lines (87 loc) · 2.23 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<iostream>
#include<math.h>
using namespace std;
//Time Complexity is O(t*n^2) and Space Complexity is O(n)
int main()
{
int n;
int t;
cout<<"Enter number of test Case:\n";
cin>>t;
for(int i=0;i<t;i++)
{
cout<<"Enter the number of elements:\n";
cin>>n;
int *ar=new int[n];
cout<<"Enter the value of array:\n";
for(int j=0;j<n;j++)
{
cin>>ar[j];
}
int *cumsum=new int[n];
cumsum[0]=ar[0];
for(int j=1;j<n;j++)
{
cumsum[j]=cumsum[j-1]+ar[j];
}
bool w=false;
for(int j=0;j<n && w==false;j++)
{
int x=cumsum[j];
if(x%2==0)
{
for(int k=j+1;k<n;k++)
{
if(cumsum[k]%2==0)
{
w=true;
cout<<k-j<<endl;
for(int l=j+1;l<=k;l++)
{
cout<<l+1<<" ";
}
cout<<endl;
break;
}
}
}
else
{
for(int k=j+1;k<n;k++)
{
if(cumsum[k]%2==1)
{
w=true;
cout<<k-j<<endl;
for(int l=j+1;l<=k;l++)
{
cout<<l+1<<" ";
}
cout<<endl;
break;
}
}
}
}
if(w==false)
{
int x=0;
for(int j=0;j<n;j++)
{
if(cumsum[j]%2==0)
{
w=true;
cout<<j-x+1<<endl;
for(int k=0;k<=j;k++)
{
cout<<k+1<<" ";
}
cout<<endl;
}
}
if(w==false){
cout<<"-1"<<endl;
}
}
}
}