Find four distinct numbers A, B, C, and D satisfying (((A|B)& C)^ D) = N
Last Updated :
10 Jul, 2023
Given a number N, the task is to find four distinct numbers A, B, C, and D such that all are distinct and non-negative satisfies the condition: (((A|B)& C)^ D) = N, where ‘ | ‘ is Bitwise ‘or’, ‘&’ is Bitwise ‘and’, ‘^’ is Bitwise ‘xor’.
Examples:
Input: N = 5
Output: A = 15, B = 0, C = 16, D = 5
Input: N = 16
Output: A = 0, B = 4, C = 3, D = 16
A = 0 , B = 63 , C = 64 , D = 16
Naive Approach: One approach to solving the given problem is to use brute force to generate all possible combinations of four distinct non-negative integers A, B, C, and D, and check whether they satisfy the given condition (((A|B)& C)^ D) = N or not. However, this approach will not be efficient for larger values of N, and hence we need a better algorithmic approach.
Efficient Approach: This can be solved with the following idea:
- We can do this by bit manipulation concept. Let’s assume C is a number of power of two. We can make (A|B) & C equal to 0. For that A|B must be equal to (C-1). n & (n-1) = 0, when n is the power of two.
- For that, we can make A or B equal to C – 1 and another equal to 0. By doing this we get (A|B) & C = 0. Now we know a number of two numbers is equal to 0 when the number is the same, as A^A = 0; 0^A = A. So D must be equal to N.
- If N == 0 then a special case arises, then we have to find A as a power of two and B = A – 1. By that, we can find an integer X-1 where X is the power of two. for that, we can make C = X and D = 0. And we can make the answer.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int bitcount( int n) { return ( int )log2(n) + 1; }
void FindSquad( int N)
{
int a, b, c, d;
int power = bitcount(N) + 1;
c = (1 << power);
a = 0;
b = c - 1;
d = N;
if (N == 0) {
a = 4;
b = 3;
c = 8;
d = 0;
}
cout << "A = " << a << " "
<< ", B = " << b << " "
<< ", C = " << c << " "
<< ", D = " << d << endl;
return ;
}
signed main()
{
int n = 5;
FindSquad(n);
return 0;
}
|
Java
import java.util.*;
class Main {
static int bitcount( int n)
{
return ( int )(Math.log(n) / Math.log( 2 )) + 1 ;
}
static void FindSquad( int N)
{
int a, b, c, d;
int power = bitcount(N) + 1 ;
c = ( 1 << power);
a = 0 ;
b = c - 1 ;
d = N;
if (N == 0 ) {
a = 4 ;
b = 3 ;
c = 8 ;
d = 0 ;
}
System.out.println( "A = " + a + ", B = " + b
+ ", C = " + c + ", D = " + d);
}
public static void main(String[] args)
{
int n = 5 ;
FindSquad(n);
}
}
|
Python3
import math
def bitcount(n):
return ( int )(math.log2(n)) + 1
def FindSquad(N):
a, b, c, d = 0 , 0 , 0 , 0
power = bitcount(N) + 1
c = ( 1 << power)
a = 0
b = c - 1
d = N
if (N = = 0 ):
a = 4
b = 3
c = 8
d = 0
print ( "A = " , a, ", B = " , b, ", C = " , c, ", D = " , d)
if __name__ = = "__main__" :
n = 5
FindSquad(n)
|
C#
using System;
public class Program
{
static int bitcount( int n)
{
return ( int )Math.Log(n, 2) + 1;
}
static void FindSquad( int N)
{
int a, b, c, d;
int power = bitcount(N) + 1;
c = (1 << power);
a = 0;
b = c - 1;
d = N;
if (N == 0)
{
a = 4;
b = 3;
c = 8;
d = 0;
}
Console.WriteLine( "A = {0}, B = {1}, C = {2}, D = {3}" , a, b, c, d);
}
public static void Main( string [] args)
{
int n = 5;
FindSquad(n);
}
}
|
Javascript
function bitCount(n) {
return Math.floor(Math.log2(n)) + 1;
}
function findSquad(N) {
let a, b, c, d;
const power = bitCount(N) + 1;
c = 1 << power;
a = 0;
b = c - 1;
d = N;
if (N === 0) {
a = 4;
b = 3;
c = 8;
d = 0;
}
console.log(`A = ${a}, B = ${b}, C = ${c}, D = ${d}`);
}
const n = 5;
findSquad(n);
|
Output
A = 0, B = 15, C = 16, D = 5
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...