📜  范围内的最大按位或对

📅  最后修改于: 2021-05-25 07:59:13             🧑  作者: Mango

给定范围[L,R] ,任务是找到一个对(X,Y) ,使L≤X,Y≤R并且(X | Y)在所有可能的对中最大,然后打印该对的按位或找到一对。

例子:

天真的方法:LR进行迭代,并检查每个可能的对的按位“或”并在最后打印最大值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#include 
using namespace std;
  
// Function to return the maximum bitwise OR
// possible among all the possible pairs
int maxOR(int L, int R)
{
    int maximum = INT_MIN;
  
    // Check for every possible pair
    for (int i = L; i < R; i++)
        for (int j = i + 1; j <= R; j++)
  
            // Maximum among all (i, j) pairs
            maximum = max(maximum, (i | j));
  
    return maximum;
}
  
// Driver code
int main()
{
    int L = 4, R = 5;
  
    cout << maxOR(L, R);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to return the maximum bitwise OR
// possible among all the possible pairs
static int maxOR(int L, int R)
{
    int maximum = Integer.MIN_VALUE;
  
    // Check for every possible pair
    for (int i = L; i < R; i++)
        for (int j = i + 1; j <= R; j++)
  
            // Maximum among all (i, j) pairs
            maximum = Math.max(maximum, (i | j));
  
    return maximum;
}
  
// Driver code
public static void main(String []args) 
{
    int L = 4, R = 5;
  
    System.out.println(maxOR(L, R));
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
  
# Function to return the maximum bitwise OR
# possible among all the possible pairs
def maxOR(L, R):
    maximum = -10**9
  
    # Check for every possible pair
    for i in range(L, R):
        for j in range(i + 1, R + 1):
  
            # Maximum among all (i, j) pairs
            maximum = max(maximum, (i | j))
  
    return maximum
  
# Driver code
L = 4
R = 5
  
print(maxOR(L, R))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
      
class GFG
{
  
// Function to return the maximum bitwise OR
// possible among all the possible pairs
static int maxOR(int L, int R)
{
    int maximum = int.MinValue;
  
    // Check for every possible pair
    for (int i = L; i < R; i++)
        for (int j = i + 1; j <= R; j++)
  
            // Maximum among all (i, j) pairs
            maximum = Math.Max(maximum, (i | j));
  
    return maximum;
}
  
// Driver code
public static void Main(String []args) 
{
    int L = 4, R = 5;
  
    Console.WriteLine(maxOR(L, R));
}
}
  
// This code is contributed by Rajput-Ji


C++
// C++ implementation of the approach
#include 
using namespace std;
  
const int MAX = 64;
  
// Function to return the maximum bitwise OR
// possible among all the possible pairs
int maxOR(int L, int R)
{
  
    // If there is only a single value
    // in the range [L, R]
    if (L == R) {
        return L;
    }
  
    int ans = 0;
  
    // Loop through each bit from MSB to LSB
    for (int i = MAX - 1; i >= 0; i--) {
        int p, lbit, rbit;
        p = 1 << i;
        lbit = (L >> i) & 1; // bit of left limit
        rbit = (R >> i) & 1; // bit of right limit
  
        // MSBs where the bits differ,
        // all bits from that bit are set
        if ((rbit == 1) && (lbit == 0)) {
            ans += (p << 1) - 1;
            break;
        }
  
        // If MSBs are same, then ans
        // bit is same as that of
        // bit of right or left limit
        if (rbit == 1) {
            ans += p;
        }
    }
    return ans;
}
  
// Driver code
int main()
{
    int L = 4, R = 5;
  
    cout << maxOR(L, R);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
static int MAX = 64;
  
// Function to return the maximum bitwise OR
// possible among all the possible pairs
static int maxOR(int L, int R)
{
  
    // If there is only a single value
    // in the range [L, R]
    if (L == R) 
    {
        return L;
    }
  
    int ans = 0;
  
    // Loop through each bit from MSB to LSB
    for (int i = MAX - 1; i >= 0; i--)
    {
        int p, lbit, rbit;
        p = 1 << i;
        lbit = (L >> i) & 1; // bit of left limit
        rbit = (R >> i) & 1; // bit of right limit
  
        // MSBs where the bits differ,
        // all bits from that bit are set
        if ((rbit == 1) && (lbit == 0)) 
        {
            ans += (p << 1) - 1;
            break;
        }
  
        // If MSBs are same, then ans
        // bit is same as that of
        // bit of right or left limit
        if (rbit == 1) 
        {
            ans += p;
        }
    }
    return ans;
}
  
// Driver code
public static void main(String[] args) 
{
    int L = 4, R = 5;
  
    System.out.println(maxOR(L, R));
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach 
  
MAX = 64; 
  
# Function to return the maximum bitwise OR 
# possible among all the possible pairs 
def maxOR(L, R) : 
  
    # If there is only a single value 
    # in the range [L, R] 
    if (L == R) :
        return L; 
  
    ans = 0; 
  
    # Loop through each bit from MSB to LSB 
    for i in range(MAX - 1, -1, -1) :
        p = 1 << i; 
        lbit = (L >> i) & 1; # bit of left limit 
        rbit = (R >> i) & 1; # bit of right limit 
  
        # MSBs where the bits differ, 
        # all bits from that bit are set 
        if ((rbit == 1) and (lbit == 0)) :
            ans += (p << 1) - 1; 
            break; 
  
        # If MSBs are same, then ans 
        # bit is same as that of 
        # bit of right or left limit 
        if (rbit == 1) :
            ans += p; 
   
    return ans; 
  
# Driver code 
if __name__ == "__main__" : 
  
    L = 4; R = 5; 
  
    print(maxOR(L, R)); 
  
    # This code is contributed by kanugargng


C#
// C# implementation of the above approach 
using System;
      
class GFG 
{
static int MAX = 64;
  
// Function to return the maximum bitwise OR
// possible among all the possible pairs
static int maxOR(int L, int R)
{
  
    // If there is only a single value
    // in the range [L, R]
    if (L == R) 
    {
        return L;
    }
  
    int ans = 0;
  
    // Loop through each bit from MSB to LSB
    for (int i = MAX - 1; i >= 0; i--)
    {
        int p, lbit, rbit;
        p = 1 << i;
        lbit = (L >> i) & 1; // bit of left limit
        rbit = (R >> i) & 1; // bit of right limit
  
        // MSBs where the bits differ,
        // all bits from that bit are set
        if ((rbit == 1) && (lbit == 0)) 
        {
            ans += (p << 1) - 1;
            break;
        }
  
        // If MSBs are same, then ans
        // bit is same as that of
        // bit of right or left limit
        if (rbit == 1) 
        {
            ans += p;
        }
    }
    return ans;
}
  
// Driver code
public static void Main(String[] args) 
{
    int L = 4, R = 5;
  
    Console.WriteLine(maxOR(L, R));
}
}
  
// This code is contributed by PrinciRaj1992


输出:
5

时间复杂度: O(n 2 )

有效的方法:或操作设定为i,如果两个操作数的有第i位设置位。我们的目标是最大化设置位的数量。

现在的任务是找到LR不同的最高有效位B。B位将在R中设置,而不会在L中设置。可以产生的最大OR将使对B位有效的所有位都与LR相同,并且对所有不如B有效位的位都将被设置,因为它们将包含在范围内。 OR中的B位将为1 | 1 | 0将为1。

现在,考虑从最高有效位(MSB)到最低有效位(LSB)的LR的二进制表示形式。假设LR的前x位相同。然后,所有可能的A和B的前x位都与LR相同,因为即使这些x位中的一个位发生变化,该值也应移出不允许的范围[L,R]

接下来,将设置所有比差位B低的有效位,包括B。

考虑以下示例,
L = 001100010
R = 001100110

LR的前6位相同,因此所有数字N(使L≤N 成立)的前6位与L (或R )相同,并且该范围内任意两个数的OR均相同前六位。

现在,忽略前x位,我们发现该位不匹配。在这一点上,可以构造A,使得不为A设置不匹配的位,并且设置所有比当前位低的有效位。类似地,可以构造B,以便为B设置不匹配位,而在B中不设置所有后续位。

它们的“或”应设置所有比特,包括不匹配比特。
将此逻辑应用于我们的示例,我们有A = 001100011和B = 001100100,它们的OR为001100111,这是最大可能的。

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
  
const int MAX = 64;
  
// Function to return the maximum bitwise OR
// possible among all the possible pairs
int maxOR(int L, int R)
{
  
    // If there is only a single value
    // in the range [L, R]
    if (L == R) {
        return L;
    }
  
    int ans = 0;
  
    // Loop through each bit from MSB to LSB
    for (int i = MAX - 1; i >= 0; i--) {
        int p, lbit, rbit;
        p = 1 << i;
        lbit = (L >> i) & 1; // bit of left limit
        rbit = (R >> i) & 1; // bit of right limit
  
        // MSBs where the bits differ,
        // all bits from that bit are set
        if ((rbit == 1) && (lbit == 0)) {
            ans += (p << 1) - 1;
            break;
        }
  
        // If MSBs are same, then ans
        // bit is same as that of
        // bit of right or left limit
        if (rbit == 1) {
            ans += p;
        }
    }
    return ans;
}
  
// Driver code
int main()
{
    int L = 4, R = 5;
  
    cout << maxOR(L, R);
  
    return 0;
}

Java

// Java implementation of the approach
class GFG 
{
static int MAX = 64;
  
// Function to return the maximum bitwise OR
// possible among all the possible pairs
static int maxOR(int L, int R)
{
  
    // If there is only a single value
    // in the range [L, R]
    if (L == R) 
    {
        return L;
    }
  
    int ans = 0;
  
    // Loop through each bit from MSB to LSB
    for (int i = MAX - 1; i >= 0; i--)
    {
        int p, lbit, rbit;
        p = 1 << i;
        lbit = (L >> i) & 1; // bit of left limit
        rbit = (R >> i) & 1; // bit of right limit
  
        // MSBs where the bits differ,
        // all bits from that bit are set
        if ((rbit == 1) && (lbit == 0)) 
        {
            ans += (p << 1) - 1;
            break;
        }
  
        // If MSBs are same, then ans
        // bit is same as that of
        // bit of right or left limit
        if (rbit == 1) 
        {
            ans += p;
        }
    }
    return ans;
}
  
// Driver code
public static void main(String[] args) 
{
    int L = 4, R = 5;
  
    System.out.println(maxOR(L, R));
}
}
  
// This code is contributed by Rajput-Ji

Python3

# Python3 implementation of the approach 
  
MAX = 64; 
  
# Function to return the maximum bitwise OR 
# possible among all the possible pairs 
def maxOR(L, R) : 
  
    # If there is only a single value 
    # in the range [L, R] 
    if (L == R) :
        return L; 
  
    ans = 0; 
  
    # Loop through each bit from MSB to LSB 
    for i in range(MAX - 1, -1, -1) :
        p = 1 << i; 
        lbit = (L >> i) & 1; # bit of left limit 
        rbit = (R >> i) & 1; # bit of right limit 
  
        # MSBs where the bits differ, 
        # all bits from that bit are set 
        if ((rbit == 1) and (lbit == 0)) :
            ans += (p << 1) - 1; 
            break; 
  
        # If MSBs are same, then ans 
        # bit is same as that of 
        # bit of right or left limit 
        if (rbit == 1) :
            ans += p; 
   
    return ans; 
  
# Driver code 
if __name__ == "__main__" : 
  
    L = 4; R = 5; 
  
    print(maxOR(L, R)); 
  
    # This code is contributed by kanugargng

C#

// C# implementation of the above approach 
using System;
      
class GFG 
{
static int MAX = 64;
  
// Function to return the maximum bitwise OR
// possible among all the possible pairs
static int maxOR(int L, int R)
{
  
    // If there is only a single value
    // in the range [L, R]
    if (L == R) 
    {
        return L;
    }
  
    int ans = 0;
  
    // Loop through each bit from MSB to LSB
    for (int i = MAX - 1; i >= 0; i--)
    {
        int p, lbit, rbit;
        p = 1 << i;
        lbit = (L >> i) & 1; // bit of left limit
        rbit = (R >> i) & 1; // bit of right limit
  
        // MSBs where the bits differ,
        // all bits from that bit are set
        if ((rbit == 1) && (lbit == 0)) 
        {
            ans += (p << 1) - 1;
            break;
        }
  
        // If MSBs are same, then ans
        // bit is same as that of
        // bit of right or left limit
        if (rbit == 1) 
        {
            ans += p;
        }
    }
    return ans;
}
  
// Driver code
public static void Main(String[] args) 
{
    int L = 4, R = 5;
  
    Console.WriteLine(maxOR(L, R));
}
}
  
// This code is contributed by PrinciRaj1992
输出:
5

时间复杂度: O(log 2 (R)),其中R是范围的上限。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。