Largest number M having bit count of N such that difference between their OR and XOR value is maximized
Given a natural number N, the task is to find the largest number M having the same length in binary representation as N such that the difference between N | M and N ^ M is maximum.
Examples:
Input: N = 6
Output: 7
Explanation:
All number numbers having same length in binary representation as N are 4, 5, 6, 7.
(6 | 4) – (6 ^ 4) = 4
(6 | 5) – (6 ^ 5) = 4
(6 | 6) – (6 ^ 6) = 6
(6 | 7) – (6 ^ 7) = 6
Hence, largest M for which (N | M) – (N ^ M) is maximum is 7
Input: N = 10
Output: 15
Explanation:
The largest number M = 15 which has the same length in binary representation as 10 and the difference between N | M and N ^ M is maximum.
Naive Approach: The idea is to simply find all the numbers having the same length in binary representation as N and then for every number iterate and find the largest integer having (N | i) – (N ^ i) maximum.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The idea is to initialize M = 0 and iterate bit by bit in N (say i) and set or unset the ith bit of M according to the following 2 observations :
- When an ith bit of N is set: In this case, if we unset the ith bit of M, ith bit of both N | M and N^M will be set whereas on setting this bit of M, an ith bit of N|M will be set and N^M will be unset which will increase (N | M) – (N ^ M). Hence, it is optimal to set this bit of M.
- When an ith bit of N is unset: In this case, if we set this bit of M, both N|M and N^M will have this bit set or on keeping this bit of M unset both N|M and N^M will have this bit unset. So, in this case, we cannot increase the difference between them but as the requirement is to output the maximum M possible, so set this bit of M.
- From the above observations, it is clear that M will have all the bits set.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxORminusXOR( int N)
{
int MSB = log2(N);
int M = 0;
for ( int i = 0; i <= MSB; i++)
M += (1 << i);
return M;
}
int main()
{
int N = 10;
cout << maxORminusXOR(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxORminusXOR( int N)
{
int MSB = ( int )Math.ceil(Math.log(N));
int M = 0 ;
for ( int i = 0 ; i <= MSB; i++)
M += ( 1 << i);
return M;
}
public static void main(String[] args)
{
int N = 10 ;
System.out.print(maxORminusXOR(N));
}
}
|
Python3
import math
def maxORminusXOR(N):
MSB = int (math.log2(N));
M = 0
for i in range (MSB + 1 ):
M + = ( 1 << i)
return M
if __name__ = = '__main__' :
N = 10
print (maxORminusXOR(N))
|
C#
using System;
class GFG{
static int maxORminusXOR( int N)
{
int MSB = ( int )Math.Ceiling(Math.Log(N));
int M = 0;
for ( int i = 0; i <= MSB; i++)
M += (1 << i);
return M;
}
public static void Main(String[] args)
{
int N = 10;
Console.Write(maxORminusXOR(N));
}
}
|
Javascript
<script>
function maxORminusXOR(N)
{
let MSB = Math.ceil(Math.log(N));
let M = 0;
for (let i = 0; i <= MSB; i++)
M += (1 << i);
return M;
}
let N = 10;
document.write(maxORminusXOR(N));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
20 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...