Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
Last Updated :
06 Sep, 2022
Given a non-negative integer N, the task is to find two integers A (greatest integer smaller than N) and (smallest integer greater than N) such that A + N = A ^ N and B + N = B ^ N
Examples:
Input: N = 5
Output: A = 2 and B = 8
2 + 8 = 2 ^ 8 = 10
Input: N = 10
Output: A = 5 and B = 16
5 + 16 = 5 ^ 16 = 21
Approach: Lets find A and B independently. To solve this problem we have to use the property, x + y = x^y + 2 * (x & y)
Since the problem states that xor sum is equal to the given sum which implies that their AND must be 0.
- Finding A: N can be represented as a series of bits of 0 and 1. To find A we will first have to find the most significant bit of N which is set. Since A & N = 0, The places where N has set bit, for that places we will make bits of A as unset and for the places where N has unset bit, we will make that bit set for A as we want to maximize A. This we will do for all the bits from most significant to the least significant. Hence we will get our A.
- Finding B: Finding B is easy. Let i be the position of the leftmost set bit in 1. We want B to be greater than N, also we want B & N =0. Hence using these two facts B will be always (1<< (i+1)).
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
#define MAX 32
void findAB( int N)
{
bitset<MAX> arr(N), brr(N);
int leftsetN = -1;
for ( int i = MAX - 1; i >= 0; --i) {
if (arr[i] == 1) {
leftsetN = i;
break ;
}
}
int A = 0;
for ( int i = leftsetN; i >= 0; --i) {
if (arr[i] == 0) {
A |= (1 << i);
}
}
int B = 0;
B = 1 << (leftsetN + 1);
cout << "A = " << A << " and B = " << B;
}
int main()
{
int N = 5;
findAB(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int MAX = 32 ;
static String Reverse(String s)
{
StringBuilder rev = new StringBuilder();
rev.append(s);
rev.reverse();
return rev.toString();
}
static void findAB( int N)
{
BitSet arr = BitSet.valueOf( new long []{N});
int leftsetN = - 1 ;
for ( int i = 63 ; i >= 0 ; --i) {
if (arr.get(i)) {
leftsetN = i;
break ;
}
}
int A = 0 ;
for ( int i = leftsetN; i >= 0 ; --i) {
if (!arr.get(i)) {
A |= ( 1 << i);
}
}
int B = 0 ;
B = 1 << (leftsetN + 1 );
System.out.println( "A = " + A + " and B = " + B);
}
public static void main(String[] args)
{
int N = 5 ;
findAB(N);
}
}
|
Python3
MAX = 32
def findAB(N):
arr = bin (N)[ 2 ::]
arr = "0" * ( MAX - len (arr)) + arr
arr = arr[:: - 1 ]
leftsetN = - 1 ;
for i in range ( MAX - 1 , - 1 , - 1 ):
if (arr[i] = = '1' ):
leftsetN = i;
break ;
A = 0 ;
for i in range (leftsetN, - 1 , - 1 ):
if (arr[i] = = '0' ) :
A | = ( 1 << (i));
B = 0 ;
B = 1 << ((leftsetN) + 1 );
print ( "A =" , A, "and B =" , B);
N = 5 ;
findAB(N);
|
C#
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
class GFG {
static int MAX = 32;
static string Reverse( string s)
{
char [] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string (charArray);
}
static void findAB( int N)
{
BitVector32 arr = new BitVector32(N);
string arrs = arr.ToString();
arrs
= Reverse(arrs.Substring(arrs.Length - 33, 32));
int leftsetN = -1;
for ( int i = MAX - 1; i >= 0; --i) {
if (arrs[i] == '1' ) {
leftsetN = i;
break ;
}
}
int A = 0;
for ( int i = leftsetN; i >= 0; --i) {
if (arrs[i] == '0' ) {
A |= (1 << i);
}
}
int B = 0;
B = 1 << (leftsetN + 1);
Console.WriteLine( "A = " + A + " and B = " + B);
}
public static void Main( string [] args)
{
int N = 5;
findAB(N);
}
}
|
Javascript
let MAX = 32
function findAB(N)
{
let arr = N.toString(2);
arr = "0" .repeat(MAX - arr.length) + arr;
arr = arr.split( "" );
arr.reverse()
let leftsetN = -1;
for (let i = MAX - 1; i >= 0; --i) {
if (arr[i] == '1' ) {
leftsetN = i;
break ;
}
}
let A = 0;
for (let i = leftsetN; i >= 0; --i) {
if (arr[i] == '0' ) {
A |= (1 << (i));
}
}
let B = 0;
B = 1 << ((leftsetN) + 1);
console.log( "A = " + A + " and B = " + B);
}
let N = 5;
findAB(N);
|
Time Complexity: O(MAX)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...