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. Examples :
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
Approach :
- 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 :
C++
#include <bits/stdc++.h>
using namespace std;
class palindrome_selfie {
public :
set< int > all_permutes;
int number;
palindrome_selfie( int num) { number = num; }
int palindrome( int num)
{
int reversednum = 0;
int d;
while (num > 0) {
d = num % 10;
reversednum = reversednum * 10 + d;
num = num / 10;
}
return reversednum;
}
int swap( int num, int i, int j)
{
char temp;
string charArray = to_string(num);
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return stoi(charArray);
}
void permute( int num, int l, int r)
{
if (l == r)
all_permutes.insert(num);
else {
for ( int i = l; i <= r; i++) {
num = swap(num, l, i);
permute(num, l + 1, r);
num = swap(num, l, i);
}
}
}
void palin_selfie()
{
int l = to_string(number).length() - 1;
permute(number, 0, l);
auto n1 = all_permutes.find(palindrome(number));
auto n2 = all_permutes.find(number);
all_permutes.erase(n1);
all_permutes.erase(n2);
bool flag = false ;
for ( auto it : all_permutes) {
int number2 = it;
if (number * palindrome(number)
== number2 * palindrome(number2)) {
cout << "Palindrome multiplicative"
<< "selfie of " << number
<< " is : " << number2 << endl;
flag = true ;
break ;
}
}
if (flag == false ) {
cout << "Given number has no palindrome selfie."
<< endl;
}
}
};
int main()
{
palindrome_selfie example1(145572);
example1.palin_selfie();
palindrome_selfie example2(19362);
example2.palin_selfie();
palindrome_selfie example3(4669);
example3.palin_selfie();
return 0;
}
|
Java
import java.util.*;
public class palindrome_selfie {
Set<Integer> all_permutes = new HashSet<Integer>();
int number;
public palindrome_selfie( int num)
{
number = num;
}
public int palindrome( int num)
{
int reversednum = 0 ;
int d;
while (num > 0 ) {
d = num % 10 ;
reversednum = reversednum * 10 + d;
num = num / 10 ;
}
return reversednum;
}
public void palin_selfie()
{
int l = String.valueOf(number).length() - 1 ;
this .permute(number, 0 , l);
all_permutes.remove(palindrome(number));
all_permutes.remove(number);
boolean flag = false ;
Iterator it = all_permutes.iterator();
while (it.hasNext()) {
int number2 = ( int )it.next();
if (number * palindrome(number) ==
number2 * palindrome(number2)) {
System.out.println( "Palindrome multiplicative" +
"selfie of " + number + " is : "
+ number2);
flag = true ;
break ;
}
}
if (flag == false ) {
System.out.println( "Given number has no palindrome selfie." );
}
}
public void permute( int num, int l, int r)
{
if (l == r)
all_permutes.add(num);
else {
for ( int i = l; i <= r; i++) {
num = swap(num, l, i);
permute(num, l + 1 , r);
num = swap(num, l, i);
}
}
}
public int swap( int num, int i, int j)
{
char temp;
char [] charArray = String.valueOf(num).toCharArray();
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return Integer.valueOf(String.valueOf(charArray));
}
public static void main(String args[])
{
palindrome_selfie example1 = new palindrome_selfie( 145572 );
example1.palin_selfie();
palindrome_selfie example2 = new palindrome_selfie( 19362 );
example2.palin_selfie();
palindrome_selfie example3 = new palindrome_selfie( 4669 );
example3.palin_selfie();
}
}
|
Python3
class palindrome_selfie:
def __init__( self , num):
self .all_permutes = set ();
self .number = num;
def palindrome( self , num):
reversednum = 0 ;
while (num > 0 ):
d = num % 10 ;
reversednum = reversednum * 10 + d;
num = (num / / 10 );
return reversednum;
def swap( self , num, i, j):
charArray = list ( str (num))
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return int ("".join(charArray))
def permute( self , num, l, r):
if (l = = r):
self .all_permutes.add(num);
else :
for i in range (l, r + 1 ):
num = self .swap(num, l, i);
self .permute(num, l + 1 , r);
num = self .swap(num, l, i);
def palin_selfie( self ):
l = len ( str ( self .number)) - 1
self .permute( self .number, 0 , l);
self .all_permutes.remove( self .palindrome( self .number));
self .all_permutes.remove( self .number);
flag = False ;
for it in self .all_permutes:
number2 = it;
if ( self .number * self .palindrome( self .number) = = number2 * self .palindrome(number2)):
print ( "Palindrome multiplicative selfie of" , self .number, "is :" , number2);
flag = True ;
break ;
if (flag = = False ):
print ( "Given number has no palindrome selfie." );
n2 = palindrome_selfie( 19362 );
n2.palin_selfie();
n3 = palindrome_selfie( 4669 );
n3.palin_selfie();
|
C#
using System;
using System.Collections.Generic;
public class palindrome_selfie
{
HashSet< int > all_permutes = new HashSet< int >();
int number;
public palindrome_selfie( int num)
{
number = num;
}
public int palindrome( int num)
{
int reversednum = 0;
int d;
while (num > 0)
{
d = num % 10;
reversednum = reversednum * 10 + d;
num = num / 10;
}
return reversednum;
}
public void palin_selfie()
{
int l = String.Join( "" ,number).Length - 1;
this .permute(number, 0, l);
all_permutes.Remove(palindrome(number));
all_permutes.Remove(number);
bool flag = false ;
foreach ( var number2 in all_permutes)
{
if (number * palindrome(number) ==
number2 * palindrome(number2))
{
Console.WriteLine( "Palindrome multiplicative" +
"selfie of " + number + " is : "
+ number2);
flag = true ;
break ;
}
}
if (flag == false )
{
Console.WriteLine( "Given number has " +
"no palindrome selfie." );
}
}
public void permute( int num, int l, int r)
{
if (l == r)
all_permutes.Add(num);
else
{
for ( int i = l; i <= r; i++)
{
num = swap(num, l, i);
permute(num, l + 1, r);
num = swap(num, l, i);
}
}
}
public int swap( int num, int i, int j)
{
char temp;
char [] charArray = String.Join( "" ,num).ToCharArray();
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return int .Parse(String.Join( "" ,charArray));
}
public static void Main(String []args)
{
palindrome_selfie example1 = new palindrome_selfie(145572);
example1.palin_selfie();
palindrome_selfie example2 = new palindrome_selfie(19362);
example2.palin_selfie();
palindrome_selfie example3 = new palindrome_selfie(4669);
example3.palin_selfie();
}
}
|
Javascript
let all_permutes;
let number;
function init(num)
{
all_permutes = new Set();
number = num;
}
function palindrome(num)
{
let reversednum = 0;
let d;
while (num > 0) {
d = num % 10;
reversednum = reversednum * 10 + d;
num = Math.floor(num / 10);
}
return reversednum;
}
function swap(num, i, j)
{
let temp;
let charArray = (num.toString()).split( "" );
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return parseInt(charArray.join( "" ));
}
function permute(num, l, r)
{
if (l == r)
all_permutes.add(num);
else {
for ( var i = l; i <= r; i++) {
num = swap(num, l, i);
permute(num, l + 1, r);
num = swap(num, l, i);
}
}
}
function palin_selfie()
{
var l = (number.toString()).length - 1;
permute(number, 0, l);
all_permutes. delete (palindrome(number));
all_permutes. delete (number);
let flag = false ;
for ( var it of all_permutes) {
let number2 = it;
if (number * palindrome(number)
== number2 * palindrome(number2)) {
console.log(
"Palindrome multiplicative selfie of "
+ number + " is : " + number2);
flag = true ;
break ;
}
}
if (flag == false ) {
console.log(
"Given number has no palindrome selfie." );
}
}
init(145572);
palin_selfie();
init(19362);
palin_selfie();
init(4669);
palin_selfie();
|
Output :
Palindrome multiplicative selfie of 145572 is : 157452
Given number has no palindrome selfie.
Palindrome multiplicative selfie of 4669 is : 6496
Time complexity: O(n*n!) where n is the number of digits in the input number.
Auxiliary Space: O(n!)
|