-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMake_Even.cpp
50 lines (50 loc) · 1.31 KB
/
Make_Even.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
47
48
49
50
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int n = stoi(s);
if(n%2==0)
{
cout<<0<<endl;
continue;
}
if(s.size()==1 && ((s[0]-'0')%2==0 )) //single digit even
{
cout<<0<<endl;
}
else if(s.size()==1 && ((s[0]-'0')%2)) //single digit odd
{
cout<<-1<<endl;
}
else
{
int e=0, x= s.size();
for(int i=0;i<s.size();i++)
{
if((s[i]-'0') %2 ==0)
e++;
}
if(e==0)
{
cout<<-1<<endl; //doesn't contain even digit
}
else
{
if((s[0]-'0')%2 && (s[x-1]-'0')%2) //first and last digit both r odd
{
cout<<2<<endl;
}
else if( (s[0]-'0')%2==0 && (s[x-1]-'0')%2 ) //first digit is even and last digit is odd
{
cout<<1<<endl;
}
}
}
}
}