Palindromic Selfie Numbers
Given a number x, find it’s palindromic selfie number according to selfie multiplicative rule. If such a number doesn’t exist, then print “No such number exists”.
A Palindromic selfie number satisfies the selfie multiplicative rule such that there exists another number y with x * reverse_digits_of(x) = y * reverse_digits_of(y), with the condition that the number y is obtained by some ordering of the digits in x, i.e x and y should have same digits with different order.
Input : 1224 Output : 2142 Explanation : Because, 1224 X 4221 = 2142 X 2412 And all digits of 2142 are formed by a different permutation of the digits in 1224 (Note: The valid output is either be 2142 or 2412) Input : 13452 Output : 14532 Explanation : Because, 13452 X 25431 = 14532 X 23541 And all digits of 14532 are formed by a different permutation of the digits in 13452 Input : 12345 Output : No such number exists Explanation : Because, with no combination of digits 1, 2, 3, 4, 5 could we get a number that satisfies 12345 X 54321 = number X reverse_of_its_digits
- The idea is to break down the number and obtain all permutations of the digits in the number.
- Then, the number and its palindrome are removed from the set of permutations obtained, which will form the LHS of our equality.
- To check for RHS, we now iterate over all other permutations equating
LHS = current_number X palindrome(current_number)
- As soon as we get a match, we exit the loop with an affirmative message, else print “No such number available”.
Below is implementation of above approach :
Palindrome multiplicative selfie of 145572 is : 157452 Given number has no palindrome selfie. Palindrome multiplicative selfie of 4669 is : 6496