Largest divisible of 3
Problem to find the which number is highest divisible of 3.
Example-1:
Input : l = [ 8, 1, 9] Output : [9, 8, 1] Explain : 981 is a largest divisible of 3.
Example-2:
Input : l = [ 8, 1, 7, 6, 0] Output : [8, 7, 6, 0] Explain : 8760 is a largest divisible of 3.
Solution
from itertools import permutations l = [ 8, 1, 9] l1 = [] n = [] for i in range(len(l),0,-1): d = list(permutations(l,i)) a = sorted(d)[::-1] for j in range(len(a)): k = [str(x) for x in a[j]] b = "".join(k) if(int(b) % 3 == 0): l1.append(b) break for y in l1[0]: n.append(int(y)) print(n)
Output
[9, 8, 1]