给定整数N ,任务是找到最大的数M ,其中( M
例子:
Input: N = 5
Output: 2
5 ^ 4 = 1 and 5 | 4 = 5. Therefore, XOR and OR between them are not equal.
5 ^ 3 = 6 and 5 | 3 = 7. Therefore, XOR and OR between them are not equal.
5 ^ 2 = 7 and 5 | 2 = 7. Therefore, XOR and OR between them are equal.
Input: N = 14
Output: 1
方法:
要获得所需的数字M ,请从N的最低有效位(LSB)到最高有效位(MSB)遍历N的所有位。这里出现两种情况:
- 如果N的第i位是1,则:
- 如果M的第i个比特被设置为1,则N ^ M将不等于N | M为(1 ^ 1 = 0)和(1 | 1 = 1)。
- 如果第i个位被设置M的为0,则N ^ M将等于N | M为(1 ^ 0 = 1)和(1 | 0 = 1)。
- 因此,如果N的第i位是1,M的第i个比特设置为0。
- 如果N的第i位是0,则:
- 如果M的第i个比特被设置为1,则N ^ M将等于N | M为(0 ^ 1 = 1)和(0 | 1 = 1)。
- 如果我们M的第i个比特设置为0,则N ^ M将等于N | M为(0 ^ 0 = 0)和(0 | 0 = 0)。
- 因此,如果M的第i个比特被设置为0或1,N ^ M将总是等于N | M。
- 由于必须找出小于N的M的最大值,因此始终将M的第i位设置为1 。
Illustration:
- N = 5
- 32-bit representation of 5 = 00000000000000000000000000000101
- LSB index of 5 = 31
- MSB index of 5 = 29
- Traversing from LSB to MSB i.e. from 31 to 29:
- For index 31, N[31] = 1. So M[31] should be set to 0.
- For index 30, N[30] = 0. So M[30] should be set to 1.
- For index 29, N[29] = 1. So M[29] should be set to 0.
- Thus the 32-bit representation of M is 00000000000000000000000000000010, which is equal to 2 in decimal representation.
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to find required
// number M
int equalXORandOR(int n)
{
// Initialising m
int m = 0;
// Finding the index of the
// most significant bit of N
int MSB = (int)log2(n);
// Calculating required number
for (int i = 0; i <= MSB; i++) {
if (!(n & (1 << i))) {
m += (1 << i);
}
}
return m;
}
// Driver Code
int main()
{
int n = 14;
cout << equalXORandOR(n);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to find required
// number M
static int equalXORandOR(int n)
{
// Initialising m
int m = 0;
// Finding the index of the
// most significant bit of N
int MSB = (int)Math.log(n);
// Calculating required number
for(int i = 0; i <= MSB; i++)
{
if ((n & (1 << i)) <= 0)
{
m += (1 << i);
}
}
return m;
}
// Driver Code
public static void main(String[] args)
{
int n = 14;
System.out.print(equalXORandOR(n));
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 program to implement
# the above approach
from math import log2
# Function to find required
# number M
def equalXORandOR(n):
# Initialising m
m = 0
# Finding the index of the
# most significant bit of N
MSB = int(log2(n))
# Calculating required number
for i in range(MSB + 1):
if(not(n & (1 << i))):
m += (1 << i)
return m
# Driver Code
n = 14
# Function call
print(equalXORandOR(n))
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find required
// number M
static int equalXORandOR(int n)
{
// Initialising m
int m = 0;
// Finding the index of the
// most significant bit of N
int MSB = (int)Math.Log(n);
// Calculating required number
for(int i = 0; i <= MSB; i++)
{
if ((n & (1 << i)) <= 0)
{
m += (1 << i);
}
}
return m;
}
// Driver Code
public static void Main(String[] args)
{
int n = 14;
Console.Write(equalXORandOR(n));
}
}
// This code is contributed by amal kumar choubey
输出:
1
时间复杂度: O(log 2 N)
辅助空间: O(1)