# Palindromic Selfie Numbers

A number is considered to be a “Selfie Number” if it can be represented using only its own digits and certain mathematical operations.

For example, 936 is a selfie number.

$$\mathrm{936\:=\:(\sqrt{9})!^{3} \:+\:6!\:=\:216\:+\:720\:=\:936}$$

Here it can be observed that a series of operations are performed on the digits of the original number and the resultant is equal to the original number.

Palindromic Selfie Numbers are a special kind of selfie number. They satisfy the selfie multiplicative rule.

• Consider a number x.

• Let the number formed by reversing the digits of x be $\mathrm{x^\prime}$.

• Let y be a number formed using the digits of x in a different order.

• Let the number formed by reversing the digits of y be $\mathrm{y^\prime}$.

Palindromic selfie numbers satisfy the following equation −

$$\mathrm{x\:×\:x^\prime\:=\:y\:×\:y^\prime}$$

## Problem Statement

For a given number x, find its palindromic selfie number according to the selfie multiplicative rule.

### Examples

Input: 1224
Output: 2142


Explanation

Given x = 1224

So $\mathrm{x^\prime}$ = 4221 is formed by reversing the digits of x

Let y = 2142. y is formed using the digits of x in a different order

So $\mathrm{y^\prime}$ = 2412 is formed by reversing the digits of y

$\mathrm{x\:×\:x^\prime}$ =1224 ×4221 = 5166504 and $\mathrm{y\:×\:y^\prime}$ = 2142 × 2412 = 5166504

Sincex× x' = y × y', y is the palindromic selfie number of x.

Input 4669:
Output: 6496


Explanation

Given x = 4669

So $\mathrm{x^\prime}$ = 9664 is formed by reversing the digits of x

Let y = 6496. y is formed using the digits of x in a different order

So $\mathrm{y^\prime}$ = 6946 is formed by reversing the digits of y

$\mathrm{x\:×\:x^\prime}$ =4669 ×9664 = 45121216 and $\mathrm{y\:×\:y^\prime}$ = 6496× 6946= 45121216

Since x× x' = y × y', y is the palindromic selfie number of x.

Input: 456
Output: No palindromic selfie number exists


Explanation

Given x = 456

So $\mathrm{x^\prime}$ = 654 is formed by reversing the digits of x

Let y = 546. y is formed using the digits of x in a different order

So $\mathrm{y^\prime}$ = 645 is formed by reversing the digits of y

$\mathrm{x\:×\:x^\prime}$ =456 ×654 = 298224 and $\mathrm{y\:×\:y^\prime}$ = 546× 645= 352170

Since $\mathrm{x\:×\:x^\prime}$ ≠ $\mathrm{y\:×\:y^\prime}$, y is not the palindromic selfie number of x.

No other permutation of 456 also satisfies the selfie multiplicative rule.

## Solution Approach

The solution approach to finding the palindromic selfie number of a given number is fairly intuitive and easy to understand.

The approach consists of the following steps −

• Define a function "reverse" that

• takes an integer as input

• converts it to a string

• reverses the string

• converts it back to an integer.

• Define a function "Swap" that

• takes an integer, i and j as input

• converts the integer to a string

• swaps the ith and jth character in the string

• converts the string back to an integer.

• Define a function "permute" that

• takes an integer, l, r and a set "permutations" as input.

• It recursively generates all possible permutations of the digits of the integer

• It stores them in the set "permutations".

• Define a function "palindromic_selfie" that

• takes an integer "num" and a set "permutations" as input.

• It generates all possible permutations of the digits of the integer "num" using the "permute" function

• It then checks if any of these permutations satisfies the palindromic selfie property by comparing the product of the number and its reverse with the product of a permutation and its reverse.

• If such a permutation is found, it returns that number. Otherwise, it returns -1.

• In the main function, set a number "n" and an empty set for storing permutations.

• Call the "palindromic_selfie" function with "n" and the empty set, and store the returned result.

• If the returned result is -1, print "No Palindromic Selfie Number Exists". Otherwise, print the returned result.

### Example: C++ Program

The following C++ program finds the palindromic selfie number of a given integer if it exists and returns it. It achieves this by finding all possible permutations of the given number using the permute() function and then using the reverse() function to determine if the given number and any permutation of the number satisfy the selfie multiplicative rule in the palindrome_selfie() function. If no such number exists it prints “No Palindrome Selfie Number Exists”.

#include <bits/stdc++.h>
using namespace std;

// Function to reverse the digits of a number
int reverse(int num){

// converting number to string
string str = to_string(num);
reverse(str.begin(), str.end());

// converting string to integer
num = stoi(str);
return num;
}

// Function that Swaps the digits i and j in the num
int Swap(int num, int i, int j){
char temp;

// converting number to string
string s = to_string(num);

// Swap the ith and jth character
temp = s[i];
s[i] = s[j];
s[j] = temp;

// Convert the string back to int and return
return stoi(s);
}

// Function to get all possible permutations of the digits in num
void permute(int num, int l, int r, set<int> &permutations){

// Adds the new permutation obtained in the set
if (l == r)
permutations.insert(num);
else{
for (int i = l; i <= r; i++){

// Swap digits to get a different ordering
int num_copy = Swap(num, l, i);

// Recurse to next pair of digits
permute(num_copy, l + 1, r, permutations);
}
}
}

// Function to check for palindrome selfie number
int palindromic_selfie(int num, set<int>& permutations) {

// Length of the number required for calculating all permutations of the digits
int l = to_string(num).length() - 1;
permute(num, 0, l, permutations); // Calculate all permutations

//Remove the number and its reverse from the obtained set as this is the LHS of multiplicative equation
auto n1 = permutations.find(reverse(num));
auto n2 = permutations.find(num);
if (n1 != permutations.end())
permutations.erase(n1);
if (n2 != permutations.end())
permutations.erase(n2);

// Go through all other permutations of the number
for (set<int>::iterator it = permutations.begin(); it != permutations.end(); it++) {
int num2 = *it;

// Check if selfie multiplicative rule holds i.e. x * reverse(x) = y * reverse(y)
if (num * reverse(num) == num2 * reverse(num2)) {
return num2;
}
}

// If no such number found
return -1;
}
int main(){
int n = 1234;
cout << "n: " << n << endl;
set<int> permutations;
int ans = palindromic_selfie(n, permutations);
if (ans == -1) {
cout << "No Palindromic Selfie Number Exists" << endl;
}
else{
cout << ans << endl;
}
return 0;
}


### Output

n: 1234
No Palindromic Selfie Number Exists


## Time and Space Complexity Analysis

### Time Complexity: O(n!)

This code has an O(n!) time complexity, where n is the input number's digit count. This is due to the fact that there are n! permutations of n digits, and the permute() method generates all potential permutations of the digits.

### Space Complexity: O(n!)

Since the set “permutations” contains all possible combinations of the digits, which equals n!, the space complexity of this code is O(n!). The space complexity of the reverse() and Swap() functions is O(n) since they also generate temporary strings of length n. The set of permutations, which has O(n!) space complexity dominates the space complexity of the overall code.

## Conclusion

Palindromic Selfie Numbers are an interesting concept in mathematics. They satisfy the selfie multiplicative equation. The article discusses an approach to find whether a number has a palindromic selfie number and if yes, it returns it. The concept of the problem, solution approach, C++ program, and the time and space complexity of the program has been analyzed thoroughly.

Updated on: 19-Apr-2023

118 Views