Largest Even Number | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Largest Even Number

The program must accept two numbers N1 and N2 as the input and it must print the largest possible even number with non-zero unit digit as the output containing all the digits from both the numbers. If an even number with non-zero unit digit cannot be formed using the digits from the two numbers, the program must print -1 as the output. Note: The last digit must not be zero. Example Input/Output 1: Input: 784201 86976 Output: 98877664102

1st Aug 2018, 8:00 AM
Gokula Krishnan S
Gokula Krishnan S - avatar
2 Answers
+ 1
even='2468' c=sorted(input()+input()) if all(i not in even for i in c): print("-1") else: j=min(range(len(c)),key=lambda i:int(c[i] not in even)) c.insert(0,c[j]) del c[j+1] print(''.join(c[::-1])) https://code.sololearn.com/cDD2F6NdwvYX/?ref=app
1st Aug 2018, 11:03 AM
Louis
Louis - avatar
0
1) append all numbers to a list. 2) sort numbers 3) iterate over the list until int(x) % 2 == 0 4) move the first number with their index and break 5) print output since i seem to get dislikes with above, I fastcoded the thing https://code.sololearn.com/cEZd8PdTNxYE/?ref=app
1st Aug 2018, 9:05 AM
Markus Kaleton
Markus Kaleton - avatar