WeCP Python Coding Question 2: Even Odd Even Odd
Problem
Statement -:
A number is
even odd special if and only if there are even digits side by side, same goes
for odd digits too. So the distributions must be in an alternating basis of
even digits and odd digits. You are given a number N, and you have to say how
many special numbers are possible <=N, and are greater than 0.
Constraints:
1<=N<=10^8
Input
Format:
First Line
containing a single integer N.
Output
Format:
Single line
containing a single integer denoting the possible number of special numbers.
Sample
Input:
30
Output:
20
Explanation:
20 such
special numbers are there :-
1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 21, 23, 25, 27, 29, 30
Program :
# WeCP
Python Coding Question 2 : Even Odd Even Odd
a=input()
ans=0
l=len(a)
def func(s):
global ans
global a
global l
S=int(s)
if int(a)>=S:
ans+=1
if len(s)==l:
return
if (S&1):
for i in range(0,9,2):
func(s+str(i))
else:
for i in range(1,10,2):
func(s+str(i))
for i in
range(2,9,2):
func(str(i))
for i in
range(1,10,2):
func(str(i))
print(ans)
For
Output Check the Video :
Comments
Post a Comment