XOR and OR of all N-digit palindrome number
Given an integer N. The task is to find the XOR and OR of all N digit palindromic numbers.
Examples
Input: 3
Output: XOR = 714 and OR = 1023
Input: 4
Output: XOR = 4606 and OR = 16383
Approach:
- Find the starting and ending number of N-digit palindromic number by:
starting number = pow(10, n - 1)
ending number = pow(10, n) - 1
- Iterate over starting number till ending number and check whether that number is palindromic or not.
- If the number is palindromic, then take XOR and OR of that number separately.
- Else proceed for next iteration and print the value of XOR and OR after all iterations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool ispalin( int num)
{
string s = to_string(num);
int st = 0, ed = s.size() - 1;
while (st <= ed) {
if (s[st] != s[ed])
return false ;
st++;
ed--;
}
return true ;
}
void CalculateXORandOR( int n)
{
int CalculateXOR = 0;
int CalculateOR = 0;
int start = pow (10, n - 1);
int end = pow (10, n) - 1;
for ( int i = start; i <= end; i++) {
if (ispalin(i)) {
CalculateXOR = CalculateXOR ^ i;
CalculateOR = CalculateOR | i;
}
}
cout << "XOR = " << CalculateXOR;
cout << " OR = " << CalculateOR;
}
int main()
{
int n = 4;
CalculateXORandOR(n);
return 0;
}
|
Java
class GFG
{
static boolean ispalin( int num)
{
String s = Integer.toString(num);
int st = 0 , ed = s.length() - 1 ;
while (st <= ed) {
if (s.charAt(st) != s.charAt(ed))
return false ;
st++;
ed--;
}
return true ;
}
static void CalculateXORandOR( int n)
{
int CalculateXOR = 0 ;
int CalculateOR = 0 ;
int start = ( int )Math.pow( 10 , n - 1 );
int end = ( int )Math.pow( 10 , n) - 1 ;
for ( int i = start; i <= end; i++) {
if (ispalin(i)) {
CalculateXOR = CalculateXOR ^ i;
CalculateOR = CalculateOR | i;
}
}
System.out.print( "XOR = " + CalculateXOR);
System.out.println( " OR = " + CalculateOR);
}
public static void main (String[] args)
{
int n = 4 ;
CalculateXORandOR(n);
}
}
|
Python3
def ispalin(num) :
s = str (num);
st = 0 ;
ed = len (s) - 1 ;
while (st < = ed) :
if (s[st] ! = s[ed]) :
return False ;
st + = 1 ;
ed - = 1 ;
return True ;
def CalculateXORandOR(n) :
CalculateXOR = 0 ;
CalculateOR = 0 ;
start = 10 * * (n - 1 );
end = ( 10 * * n) - 1 ;
for i in range ( start, end + 1 ) :
if (ispalin(i)) :
CalculateXOR = CalculateXOR ^ i;
CalculateOR = CalculateOR | i;
print ( "XOR =" , CalculateXOR,end = " " );
print ( "OR = " , CalculateOR);
if __name__ = = "__main__" :
n = 4 ;
CalculateXORandOR(n);
|
C#
using System;
class GFG
{
static bool ispalin( int num)
{
string s = num.ToString();
int st = 0;
int ed = s.Length - 1;
while (st <= ed) {
if (s[st] != s[ed])
return false ;
st++;
ed--;
}
return true ;
}
static void CalculateXORandOR( int n)
{
int CalculateXOR = 0;
int CalculateOR = 0;
int start = ( int )Math.Pow(10, n - 1);
int end = ( int )Math.Pow(10, n) - 1;
/// ending number
for ( int i = start; i <= end; i++) {
if (ispalin(i)) {
CalculateXOR = CalculateXOR ^ i;
CalculateOR = CalculateOR | i;
}
}
Console.Write( "XOR = " + CalculateXOR);
Console.WriteLine( " OR = " + CalculateOR);
}
public static void Main ( string [] args)
{
int n = 4;
CalculateXORandOR(n);
}
}
|
Javascript
<script>
function ispalin(num)
{
let s = num.toString();
let st = 0, ed = s.length - 1;
while (st <= ed) {
if (s[st] != s[ed])
return false ;
st++;
ed--;
}
return true ;
}
function CalculateXORandOR(n)
{
let CalculateXOR = 0;
let CalculateOR = 0;
let start = Math.pow(10, n - 1);
let end = Math.pow(10, n) - 1;
for (let i = start; i <= end; i++) {
if (ispalin(i)) {
CalculateXOR = CalculateXOR ^ i;
CalculateOR = CalculateOR | i;
}
}
document.write( "XOR = " + CalculateXOR);
document.write( " OR = " + CalculateOR);
}
let n = 4;
CalculateXORandOR(n);
</script>
|
Output:
XOR = 4606 OR = 16383
Time Complexity: O(10n)
Auxiliary Space: O(1)
Last Updated :
18 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...