Approach: The given problem can be solved based on the following observations:
Let N = rpq Therefore, N = 100r + 10q + p Therefore, reverse of N = 100p + 10q + r Therefore, the problem reduces to solving (100r + 10q + p) – (r + 10q + 100p) = X -> 99(r – p) = X -> r – p = X / 99 Therefore, if given X is a multiple of 99, then solution exists.
Follow the steps below to solve the problem based on the above observations:
Check if X is multiple of 99 or not. If not found to be true, print -1 as no solution exists.
Otherwise, calculate X / 99. Generate all pairs using digits [1, 9] and for each pair, check if their difference is equal to X / 99 or not.
If found to be true for any pair, increase count by 10, as the middle digit can be permuted to place any value from the range [0, 9] for the obtained pair.
Finally, print the value of the count obtained.
Below is the implementation of the above approach:
Please Login to comment...