📌  相关文章
📜  位数为N的最大数M,使得其OR和XOR值之间的差异最大

📅  最后修改于: 2021-05-25 09:12:15             🧑  作者: Mango

给定自然数N ,任务是找到二进制数中与N长度相同的最大数M ,以使N | N MN ^ M为最大值。

例子:

天真的方法:这个想法是简单地找到二进制表示形式中与N长度相同的所有数字,然后对每个数字进行迭代并找到最大的数字。 整数 具有(N | i)–(N ^ i)个最大值。

时间复杂度: O(N)
辅助空间: O(1)

高效的方法:想法是初始化M = 0并逐个迭代N(例如i ),并根据以下两个观察结果设置或取消设置M的i位:

  • 当N的第i设置:在这种情况下,如果我们取消设置M的第i位,我都N|将设置MN ^ M,而在设置M的该位时,将设置N | M的i位,而未设置N ^ M ,这将增加(N | M)–(N ^ M) 。因此,最好将M的这个位置位。
  • 当N的i位未设置时:在这种情况下,如果我们N的i位设置 M, N | M和N ^ M都将设置此位,或者在M的该位保持不变的情况下, N | MN ^ 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)