Palindromic Selfie Numbers | Java program to find palindromic selfie numbers ; To store all permutations of digits in the number ; int number ; input number ; Function to reverse the digits of a number ; Append it at the beg ; num = num / 10 ; Reduce number until 0 ; Function to check palindromic selfie ; Length of the number required for calculating all permutations of the digits ; Remove the number and its palindrome from the obtained set as this is the LHS of multiplicative equality ; Iterate over all other numbers ; Check for equality x * palin ( x ) = y * palin ( y ) ; flag = true ; Answer found ; If no such number found ; Function to get all possible possible permutations of the digits in num ; Adds the new permutation obtained in the set ; Swap digits to get a different ordering ; Recurse to next pair of digits ; num = swap ( num , l , i ) ; Swap back ; Function that swaps the digits i and j in the num ; Convert int to char array ; Swap the ith and jth character ; Convert back to int and return ; Driver Function ; First example , input = 145572 ; Second example , input = 19362 ; Third example , input = 4669
import java . util . * ; public class palindrome_selfie { Set < Integer > all_permutes = new HashSet < Integer > ( ) ; public palindrome_selfie ( int num ) { number = num ; } public int palindrome ( int num ) { int reversednum = 0 ; int d ; while ( num > 0 ) { reversednum = reversednum * 10 + d ; } return reversednum ; } public void palin_selfie ( ) { int l = String . valueOf ( number ) . length ( ) - 1 ; all_permutes . remove ( palindrome ( number ) ) ; all_permutes . remove ( number ) ; 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 ) ; 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 ) ; } } } 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 ( ) ; } }