Find the largest palindrome XOR integer smaller than n
Given an integer n, you need to find the largest integer m (m < n) such that m XOR n is a palindrome. If there is no such integer, return -1.
Examples :
Input: n = 10
Output: 5
Explanation:
- The binary representation of 10 is 1010.
- The binary representation of 5 is 0101.
The XOR of 10 and 5 is 1111, which is a palindrome.
Input: n = 7
Output: 5
Explanation:
- The binary representation of 7 is 111.
- The binary representation of 3 is 101.
The XOR of 7 and 3 is 101, which is a palindrome.
Approach: This can be solved with the following idea:
This can be solved by mathematical and logical observations.
- Finding the number of bits in the input integer n using the formula numBits = log2(n) + 1.
- It then iterates from the maximum possible value of m down to 0.
- Inside the loop, the function computes the XOR of m and n and checks if it is a palindrome. To check if the XOR is a palindrome, it starts by comparing the first and last bits of the XOR and then moves toward the middle of the XOR, comparing the corresponding bits at each end.
- The loop stops as soon as the comparison reaches the middle of the XOR. If the XOR is a palindrome, the function returns m.
- If no integer m is found such that m XOR n is a palindrome, the function returns -1.
Below is the implementation of the code :
C++
#include <bits/stdc++.h>
#include <bitset>
using namespace std;
int largestPalindromeXOR( int n)
{
int numBits = log2(n) + 1;
for ( int m = n - 1; m >= 0; m--) {
int x = m ^ n;
int i = 0, j = numBits - 1;
bool isPalindrome = true ;
while (i < j) {
if (x & (1 << i)) {
if (!(x & (1 << j))) {
isPalindrome = false ;
break ;
}
}
else {
if (x & (1 << j)) {
isPalindrome = false ;
break ;
}
}
i++;
j--;
}
if (isPalindrome) {
return m;
}
}
return -1;
}
int main()
{
int n = 10;
int largestPalXOR = largestPalindromeXOR(n);
if (largestPalXOR == -1) {
cout << "No such integer exists" << endl;
}
else {
cout << largestPalXOR << endl;
}
return 0;
}
|
Java
import java.util.*;
public class LargestPalindromeXOR {
public static int largestPalindromeXOR( int n)
{
int numBits = ( int )(Math.log(n) / Math.log( 2 )) + 1 ;
for ( int m = n - 1 ; m >= 0 ; m--) {
int x = m ^ n;
int i = 0 , j = numBits - 1 ;
boolean isPalindrome = true ;
while (i < j) {
if (((x & ( 1 << i)) != 0 )
&& ((x & ( 1 << j)) == 0 )) {
isPalindrome = false ;
break ;
}
else if (((x & ( 1 << i)) == 0 )
&& ((x & ( 1 << j)) != 0 )) {
isPalindrome = false ;
break ;
}
i++;
j--;
}
if (isPalindrome) {
return m;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int n = 10 ;
int largestPalXOR = largestPalindromeXOR(n);
if (largestPalXOR == - 1 ) {
System.out.println( "No such integer exists" );
}
else {
System.out.println(largestPalXOR);
}
}
}
|
Python3
import math
def largestPalindromeXOR(n):
numBits = int (math.log2(n)) + 1
for m in range (n - 1 , - 1 , - 1 ):
x = m ^ n
i = 0
j = numBits - 1
isPalindrome = True
while i < j:
if x & ( 1 << i):
if not (x & ( 1 << j)):
isPalindrome = False
break
else :
if x & ( 1 << j):
isPalindrome = False
break
i + = 1
j - = 1
if isPalindrome:
return m
return - 1
if __name__ = = "__main__" :
n = 10
largestPalXOR = largestPalindromeXOR(n)
if largestPalXOR = = - 1 :
print ( "No such integer exists" )
else :
print (largestPalXOR)
|
C#
using System;
class GFG {
public static int largestPalindromeXOR( int n)
{
int numBits = ( int )(Math.Log(n) / Math.Log(2)) + 1;
for ( int m = n - 1; m >= 0; m--) {
int x = m ^ n;
int i = 0, j = numBits - 1;
bool isPalindrome = true ;
while (i < j) {
if (((x & (1 << i)) != 0)
&& ((x & (1 << j)) == 0)) {
isPalindrome = false ;
break ;
}
else if (((x & (1 << i)) == 0)
&& ((x & (1 << j)) != 0)) {
isPalindrome = false ;
break ;
}
i++;
j--;
}
if (isPalindrome) {
return m;
}
}
return -1;
}
public static void Main( string [] args)
{
int n = 10;
int largestPalXOR = largestPalindromeXOR(n);
if (largestPalXOR == -1) {
Console.WriteLine( "No such integer exists" );
}
else {
Console.WriteLine(largestPalXOR);
}
}
}
|
Javascript
function largestPalindromeXOR(n) {
let numBits = Math.floor(Math.log2(n)) + 1;
for (let m = n - 1; m >= 0; m--) {
let x = m ^ n;
let i = 0, j = numBits - 1;
let isPalindrome = true ;
while (i < j) {
if (x & (1 << i)) {
if (!(x & (1 << j))) {
isPalindrome = false ;
break ;
}
} else {
if (x & (1 << j)) {
isPalindrome = false ;
break ;
}
}
i++;
j--;
}
if (isPalindrome) {
return m;
}
}
return -1;
}
let n = 10;
let largestPalXOR = largestPalindromeXOR(n);
if (largestPalXOR == -1) {
console.log( "No such integer exists" );
} else {
console.log(largestPalXOR);
}
|
Time Complexity: O(n*logn)
Auxiliary Space: O(1)
Last Updated :
22 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...