Largest Possible N - Divisible by 4
Coding Problem Keys
Largest Possible N - Divisible by 4
Problem Statement
The program must accept an integer N as the input. The program must print the largest possible integer divisible by 4 from the digits of N. If it is not possible to form such an integer, the program must print -1 as the output.
Note: The largest possible integer divisible by 4 has no leading zeros.
Boundary Condition(S)
2 <= N <= 10⁸
Input Format
The first line contains N.
Output Format
The first line contains the largest possible integer divisible by 4 from the digits of N or -1.
Example Input/Output 1
Input
102
Output
120
Explanation
Here the largest possible integer divisible by 4 from the digits of 102 is 120.
Hence the output is 120.
Example Input/Output 2
Input
1345
Output
-1
Note: Max Execution Time Limit: 500 millisecs
Solution
Programming Language: Python 3 Language
from itertools import permutations
n=input().strip()
l=list(permutations(n,len(n)))
s=[]
for i in l:
k=""
for j in i:
k+=j
if(int(k)%4==0):
s.append(int(k))
if(s==[]):
print("-1")
else:
print(max(s))
# Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments