📌  相关文章
📜  找到一个数 M < N 使得它们的 XOR 和 AND 之间的差异最大

📅  最后修改于: 2021-10-25 11:18:45             🧑  作者: Mango

给定一个自然数N ,任务是找到一个小于N的数M ,使得它们的按位 XOR ( N ^ M ) 和按位 AND ( N & M ) 之间的差异最大。

例子:

原始的方法:我们的想法是迭代的每一个元素小于N和发现M代表其中,N ^ M – N的&M最大。以下是步骤:

  • 初始化一个变量,假设maxDiff为 0,M 为 -1。
  • 从 0 迭代到 N-1 并计算diff = N^i -N&i
  • 如果 diff 大于或等于maxDiff,则分配 M = i 和 maxDiff = diff。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to return M= maxDiff) {
 
            // Update variables
            maxDiff = diff;
            M = i;
        }
    }
 
    // Return the answer
    return M;
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 6;
 
    // Function Call
    cout << getMaxDifference(N);
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to return M= maxDiff)
        {
 
            // Update variables
            maxDiff = diff;
            M = i;
        }
    }
 
    // Return the answer
    return M;
}
 
// Driver Code
public static void main(String[] args)
{
    // Given Number N
    int N = 6;
 
    // Function Call
    System.out.print(getMaxDifference(N));
}
}
 
// This code is contributed by Rohit_ranjan


Python3
# Python3 program for the above approach
 
# Function to return M= maxDiff):
             
            # Update variables
            maxDiff = diff;
            M = i;
 
    # Return the answer
    return M;
 
# Driver Code
if __name__ == '__main__':
     
    # Given number N
    N = 6;
 
    # Function call
    print(getMaxDifference(N));
 
# This code is contributed by amal kumar choubey


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to return M= maxDiff)
        {
             
            // Update variables
            maxDiff = diff;
            M = i;
        }
    }
 
    // Return the answer
    return M;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given number N
    int N = 6;
 
    // Function call
    Console.Write(getMaxDifference(N));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to flip all bits of N
int findM(int N)
{
    int M = 0;
 
    // Finding 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 the answer
    return M;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 6;
 
    // Function Call
    cout << findM(N);
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to flip all bits of N
static int findM(int N)
{
    int M = 0;
 
    // Finding 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 the answer
    return M;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given number
    int N = 6;
 
    // Function call
    System.out.print(findM(N));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the above approach
import math
 
# Function to flip all bits of N
def findM(N):
     
    M = 0;
 
    # Finding most significant bit of N
    MSB = int(math.log(N));
 
    # Calculating required number
    for i in range(MSB):
        if ((N & (1 << i)) == 0):
            M += (1 << i);
     
    # Return the answer
    return M;
 
# Driver Code
if __name__ == '__main__':
 
    # Given number
    N = 6;
 
    # Function call
    print(findM(N));
 
# This code is contributed by Amit Katiyar


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to flip all bits of N
static int findM(int N)
{
    int M = 0;
 
    // Finding 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 the answer
    return M;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given number
    int N = 6;
 
    // Function call
    Console.Write(findM(N));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
1

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

有效的方法:这个想法是观察如果两个数字的按位与是最小可能的数字并且最小可能的数字是0 ,则按位异或和按位与之间的差异是最大的。
两个数字之间的按位 AND 为零当且仅当它们彼此互补。因此, M的可能值必须是给定数字N的补码。
下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to flip all bits of N
int findM(int N)
{
    int M = 0;
 
    // Finding 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 the answer
    return M;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 6;
 
    // Function Call
    cout << findM(N);
    return 0;
}

Java

// Java program for the above approach
class GFG{
 
// Function to flip all bits of N
static int findM(int N)
{
    int M = 0;
 
    // Finding 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 the answer
    return M;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given number
    int N = 6;
 
    // Function call
    System.out.print(findM(N));
}
}
 
// This code is contributed by Rajput-Ji

蟒蛇3

# Python3 program for the above approach
import math
 
# Function to flip all bits of N
def findM(N):
     
    M = 0;
 
    # Finding most significant bit of N
    MSB = int(math.log(N));
 
    # Calculating required number
    for i in range(MSB):
        if ((N & (1 << i)) == 0):
            M += (1 << i);
     
    # Return the answer
    return M;
 
# Driver Code
if __name__ == '__main__':
 
    # Given number
    N = 6;
 
    # Function call
    print(findM(N));
 
# This code is contributed by Amit Katiyar

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to flip all bits of N
static int findM(int N)
{
    int M = 0;
 
    // Finding 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 the answer
    return M;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given number
    int N = 6;
 
    // Function call
    Console.Write(findM(N));
}
}
 
// This code is contributed by Amit Katiyar

Javascript


输出:
1

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程