给定一个自然数N ,任务是找到在二进制表示中与N具有相同长度的最大数M ,使得N | M和N^M是最大值。
例子:
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.
朴素的方法:这个想法是简单地找到所有与N具有相同长度的二进制表示的数字,然后对每个数字进行迭代并找到最大的 整数 具有(N | i) – (N ^ i)最大值。
时间复杂度: O(N)
辅助空间: O(1)
有效的方法:这个想法是初始化M = 0并在 N 中逐位迭代(比如i )并根据以下 2 个观察设置或取消设置M 的第i位:
- 当N的第i位设置:在这种情况下,如果我们取消设置M的第i位,我都第N位| M和N^M将被设置,而在设置 M 的这一位时, N|M 的第i 位将被设置并且N^M将被取消设置,这将增加(N | M) – (N ^ M) 。因此,设置M 的这一位是最佳的。
- 当 N 的第i 位未设置时:在这种情况下,如果我们设置了该位 M, N|M和 N^M 都将设置此位,或者保持 M 的此位未设置, N|M和N^M都将未设置此位。因此,在这种情况下,我们不能增加它们之间的差异,但由于要求输出最大可能的M ,因此设置M 的这一位。
- 根据以上观察,很明显M将设置所有位。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the largest number
// M having the same length in binary
// form as N such that the difference
// between N | M and N ^ M is maximum
int maxORminusXOR(int N)
{
// Find the most significant
// bit of N
int MSB = log2(N);
// Initialize M
int M = 0;
// Set all the bits of M
for (int i = 0; i <= MSB; i++)
M += (1 << i);
// Return the answer
return M;
}
// Driver Code
int main()
{
// Given Number N
int N = 10;
// Function Call
cout << maxORminusXOR(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the largest number
// M having the same length in binary
// form as N such that the difference
// between N | M and N ^ M is maximum
static int maxORminusXOR(int N)
{
// Find the most significant
// bit of N
int MSB = (int)Math.ceil(Math.log(N));
// Initialize M
int M = 0;
// Set all the bits of M
for(int i = 0; i <= MSB; i++)
M += (1 << i);
// Return the answer
return M;
}
// Driver Code
public static void main(String[] args)
{
// Given number N
int N = 10;
// Function call
System.out.print(maxORminusXOR(N));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
import math
# Function to find the largest number
# M having the same length in binary
# form as N such that the difference
# between N | M and N ^ M is maximum
def maxORminusXOR(N):
# Find the most significant
# bit of N
MSB = int(math.log2(N));
# Initialize M
M = 0
# Set all the bits of M
for i in range(MSB + 1):
M += (1 << i)
# Return the answer
return M
# Driver code
if __name__ == '__main__':
# Given Number N
N = 10
# Function call
print(maxORminusXOR(N))
# This code is contributed by jana_sayantan
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the largest number
// M having the same length in binary
// form as N such that the difference
// between N | M and N ^ M is maximum
static int maxORminusXOR(int N)
{
// Find the most significant
// bit of N
int MSB = (int)Math.Ceiling(Math.Log(N));
// Initialize M
int M = 0;
// Set all the bits of M
for(int i = 0; i <= MSB; i++)
M += (1 << i);
// Return the answer
return M;
}
// Driver Code
public static void Main(String[] args)
{
// Given number N
int N = 10;
// Function call
Console.Write(maxORminusXOR(N));
}
}
// This code is contributed by 29AjayKumar
Javascript
15
时间复杂度: O(log N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。