This is (probably) the most easiest question of the problem set..!
You will be given a number say 'X'. You just have to find out one of its permutation that is divisible by 16.
Now this is to clarify what permutation means:
Suppose you are given 'X' as '123' then all possible permutations are: '123', '132', '213', '231', '312', '321'.
Just one twist though..
You need to output the lexicographically smallest permutation of 'X' that is divisible by 16.
Note: Your smallest permutation should not start with '0' (Technically, NO LEADING ZEROES ALLOWED...!!)
Note: If there are no such permutations that are divisible by 16 then, output "-1".
Input format:
First line contains 'X' as a string. All characters will be digits from 0 to 9 inclusive.
Output format:
Output "-1", if no such permutation exists.
else Output smallest permutation with no leading zeroes.
Constraints:
1 <= X <= 100. (Note that this means 'X' can be maximum '100' digits long number..!)
There are total 24 permutations possible. But valid are only 18 (permutations starting with '0' are not valid so)..
Permutations that are divisible by 16 are: 2048, 8240, 2480, 4208.
Out of them 2048 is smallest. So answer is 2048.
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor