Given a number n, we need to rearrange all its digits such that the new arrangement is divisible by n. Also, the new number should not be equal to x. If no such rearrangement is possible, print -1.
Input : n = 1035 Output : 3105 The result 3105 is divisible by given n and has the same set of digits. Input : n = 1782 Output : m = 7128
Simple Approach : Find all the permutation of given n and then check whether it is divisible by n or not also check that new permutation should not be equal to n.
Efficient Approach : Let’s suppose that y is our result then y = m * n, also we know that y must be a rearrangement of digits of n so we can say now restrict m (the multiplier) as per given conditions.
1) y has the same number of digits as n has. So, m must be less than 10.
2) y must not be equal to n. So, m will be greater than 1.
So we get the multiplier m in the range [2,9]. So we will find all the possible y and then check that should y has the same digits as n or not.
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.