📌  相关文章
📜  找出一个M <N的数字,以使它们的XOR和AND之差最大

📅  最后修改于: 2021-05-25 04:51:00             🧑  作者: 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 signifcant 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 signifcant 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 signifcant 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 signifcant 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


输出:
1

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

高效方法:想法是,如果两个数字的按位与是最小可能数且最小可能数为0 ,则按位XOR和按位AND的差是最大的。
当且仅当两个数字彼此互补时,两个数字之间的按位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 signifcant 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 signifcant 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 signifcant 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 signifcant 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
输出:
1

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