Find a number M < N such that difference between their XOR and AND is maximum
Given a natural number N, the task is to find a number M smaller than N such that the difference between their bitwise XOR (N ^ M) and bitwise AND (N & M) is maximum.
Examples:
Input: N = 4
Output: 3
Explanation:
(4 ^ 0) – (4 & 0) = 4
(4 ^ 1) – (4 & 1) = 5
(4 ^ 2) – (4 & 2) = 6
(4 ^ 3) – (4 & 3) = 7
Hence, the value of M is 3.
Input: N = 6
Output: 1
Explanation:
The difference between N ^ M and N & M is maximum when M = 1.
Naive Approach: The idea is to iterate for every element less than N and find M for which N^M – N&M is maximum. Below are the steps:
- Initialize a variable let’s say maxDiff with 0 and M with -1.
- Iterate from 0 to N-1 and calculate diff = N^i -N&i.
- If diff is greater or equal to maxDiff assign M = i and maxDiff = diff.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getMaxDifference( int N)
{
int M = -1;
int maxDiff = 0;
for ( int i = 0; i < N; i++) {
int diff = (N ^ i) - (N & i);
if (diff >= maxDiff) {
maxDiff = diff;
M = i;
}
}
return M;
}
int main()
{
int N = 6;
cout << getMaxDifference(N);
return 0;
}
|
Java
class GFG{
static int getMaxDifference( int N)
{
int M = - 1 ;
int maxDiff = 0 ;
for ( int i = 0 ; i < N; i++)
{
int diff = (N ^ i) - (N & i);
if (diff >= maxDiff)
{
maxDiff = diff;
M = i;
}
}
return M;
}
public static void main(String[] args)
{
int N = 6 ;
System.out.print(getMaxDifference(N));
}
}
|
Python3
def getMaxDifference(N):
M = - 1 ;
maxDiff = 0 ;
for i in range (N):
diff = (N ^ i) - (N & i);
if (diff > = maxDiff):
maxDiff = diff;
M = i;
return M;
if __name__ = = '__main__' :
N = 6 ;
print (getMaxDifference(N));
|
C#
using System;
class GFG{
static int getMaxDifference( int N)
{
int M = -1;
int maxDiff = 0;
for ( int i = 0; i < N; i++)
{
int diff = (N ^ i) - (N & i);
if (diff >= maxDiff)
{
maxDiff = diff;
M = i;
}
}
return M;
}
public static void Main(String[] args)
{
int N = 6;
Console.Write(getMaxDifference(N));
}
}
|
Javascript
<script>
function getMaxDifference(N) {
var M = -1;
var maxDiff = 0;
for (i = 0; i < N; i++) {
var diff = (N ^ i) - (N & i);
if (diff >= maxDiff) {
maxDiff = diff;
M = i;
}
}
return M;
}
var N = 6;
document.write(getMaxDifference(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The idea is to observe that the difference between Bitwise XOR and Bitwise AND is maximum if Bitwise AND of the two numbers is the minimum possible number and the minimum possible number is 0.
The Bitwise AND between the two numbers is zero if and only if they complement each other. Therefore, the possible value of M must be the complement of the given number N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findM( int N)
{
int M = 0;
int MSB = ( int )log2(N);
for ( int i = 0; i < MSB; i++) {
if (!(N & (1 << i)))
M += (1 << i);
}
return M;
}
int main()
{
int N = 6;
cout << findM(N);
return 0;
}
|
Java
class GFG{
static int findM( int N)
{
int M = 0 ;
int MSB = ( int )Math.log(N);
for ( int i = 0 ; i < MSB; i++)
{
if ((N & ( 1 << i)) == 0 )
M += ( 1 << i);
}
return M;
}
public static void main(String[] args)
{
int N = 6 ;
System.out.print(findM(N));
}
}
|
Python3
import math
def findM(N):
M = 0 ;
MSB = int (math.log(N));
for i in range (MSB):
if ((N & ( 1 << i)) = = 0 ):
M + = ( 1 << i);
return M;
if __name__ = = '__main__' :
N = 6 ;
print (findM(N));
|
C#
using System;
class GFG{
static int findM( int N)
{
int M = 0;
int MSB = ( int )Math.Log(N);
for ( int i = 0; i < MSB; i++)
{
if ((N & (1 << i)) == 0)
M += (1 << i);
}
return M;
}
public static void Main(String[] args)
{
int N = 6;
Console.Write(findM(N));
}
}
|
Javascript
<script>
function findM(N) {
var M = 0;
var MSB = parseInt( Math.log(N));
for (i = 0; i < MSB; i++) {
if ((N & (1 << i)) == 0)
M += (1 << i);
}
return M;
}
var N = 6;
document.write(findM(N));
</script>
|
Time Complexity: O(log2N)
Auxiliary Space: O(1)
Last Updated :
28 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...