给定范围[L,R] ,任务是从给定范围中找到某对(a,b)的最大按位OR。
例子:
Input: L = 10, R = 20
Output: 31
Input: L = 56, R = 77
Output: 127
方法:首先,将给定的整数L和R都转换为它们的二进制表示形式。现在,如果L的位数少于R,则将零推至L的MSB侧,以使L和R的位数相等。
现在从MSB端比较L和R的各个位。当R大于L时,我们将发现R的第i位为1且L的第i位为0的情况。因此,在第i位之后,将L的所有位设为1 。这确保了在L位中完成的修改不会超过R ,因此仅在L和R之间。并且这样做还确保最大按位或。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum bitwise
// OR of any pair from the given range
long long int max_bitwise_or(long long int L, long long int R)
{
vector v1, v2, v3;
long long int z = 0, i, ans = 0, cnt = 1;
// Converting L to its binary representation
while (L > 0) {
v1.push_back(L % 2);
L = L / 2;
}
// Converting R to its binary representation
while (R > 0) {
v2.push_back(R % 2);
R = R / 2;
}
// In order to make the number
// of bits of L and R same
while (v1.size() != v2.size()) {
// Push 0 to the MSB
v1.push_back(0);
}
for (i = v2.size() - 1; i >= 0; i--) {
// When ith bit of R is 1
// and ith bit of L is 0
if (v2[i] == 1 && v1[i] == 0 && z == 0) {
z = 1;
continue;
}
// From MSB side set all bits of L to be 1
if (z == 1) {
// From (i+1)th bit, all bits
// of L changed to be 1
v1[i] = 1;
}
}
for (i = 0; i < v2.size(); i++) {
v3.push_back(v2[i] | v1[i]);
}
for (i = 0; i < v2.size(); i++) {
if (v3[i] == 1) {
ans += cnt;
}
cnt *= 2;
}
return ans;
}
// Driver code
int main()
{
long long int L = 10, R = 20;
cout << max_bitwise_or(L, R);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to return the maximum bitwise
// OR of any pair from the given range
static int max_bitwise_or(int L, int R)
{
Vector v1 = new Vector(),
v2 = new Vector(),
v3 = new Vector();
int z = 0, i, ans = 0, cnt = 1;
// Converting L to its binary representation
while (L > 0)
{
v1.add(L % 2);
L = L / 2;
}
// Converting R to its binary representation
while (R > 0)
{
v2.add(R % 2);
R = R / 2;
}
// In order to make the number
// of bits of L and R same
while (v1.size() != v2.size())
{
// Push 0 to the MSB
v1.add(0);
}
for (i = v2.size() - 1; i >= 0; i--)
{
// When ith bit of R is 1
// and ith bit of L is 0
if (v2.get(i) == 1 && v1.get(i) == 0 && z == 0)
{
z = 1;
continue;
}
// From MSB side set all bits of L to be 1
if (z == 1)
{
// From (i+1)th bit, all bits
// of L changed to be 1
v1.remove(i);
v1.add(i,1);
}
}
for (i = 0; i < v2.size(); i++)
{
v3.add(v2.get(i) | v1.get(i));
}
for (i = 0; i < v2.size(); i++)
{
if (v3.get(i) == 1)
{
ans += cnt;
}
cnt *= 2;
}
return ans;
}
// Driver code
public static void main(String []args)
{
int L = 10, R = 20;
System.out.println(max_bitwise_or(L, R));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of the approach
# Function to return the maximum bitwise
# OR of any pair from the given range
def max_bitwise_or(L, R):
v1 = []
v2 = []
v3 = []
z = 0
i = 0
ans = 0
cnt = 1
# Converting L to its binary representation
while (L > 0):
v1.append(L % 2)
L = L // 2
# Converting R to its binary representation
while (R > 0):
v2.append(R % 2)
R = R // 2
# In order to make the number
# of bits of L and R same
while (len(v1) != len(v2)):
# Push 0 to the MSB
v1.append(0)
for i in range(len(v2) - 1, -1, -1):
# When ith bit of R is 1
# and ith bit of L is 0
if (v2[i] == 1 and
v1[i] == 0 and z == 0):
z = 1
continue
# From MSB side set all bits of L to be 1
if (z == 1):
# From (i+1)th bit, all bits
# of L changed to be 1
v1[i] = 1
for i in range(len(v2)):
v3.append(v2[i] | v1[i])
for i in range(len(v2)):
if (v3[i] == 1):
ans += cnt
cnt *= 2
return ans
# Driver code
L = 10
R = 20
print(max_bitwise_or(L, R))
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the maximum bitwise
// OR of any pair from the given range
static int max_bitwise_or(int L, int R)
{
List v1 = new List(),
v2 = new List(),
v3 = new List();
int z = 0, i, ans = 0, cnt = 1;
// Converting L to its binary representation
while (L > 0)
{
v1.Add(L % 2);
L = L / 2;
}
// Converting R to its binary representation
while (R > 0)
{
v2.Add(R % 2);
R = R / 2;
}
// In order to make the number
// of bits of L and R same
while (v1.Count != v2.Count)
{
// Push 0 to the MSB
v1.Add(0);
}
for (i = v2.Count - 1; i >= 0; i--)
{
// When ith bit of R is 1
// and ith bit of L is 0
if (v2[i] == 1 && v1[i] == 0 && z == 0)
{
z = 1;
continue;
}
// From MSB side set all bits of L to be 1
if (z == 1)
{
// From (i+1)th bit, all bits
// of L changed to be 1
v1.RemoveAt(i);
v1.Insert(i, 1);
}
}
for (i = 0; i < v2.Count; i++)
{
v3.Add(v2[i] | v1[i]);
}
for (i = 0; i < v2.Count; i++)
{
if (v3[i] == 1)
{
ans += cnt;
}
cnt *= 2;
}
return ans;
}
// Driver code
public static void Main(String []args)
{
int L = 10, R = 20;
Console.WriteLine(max_bitwise_or(L, R));
}
}
// This code is contributed by Rajput-Ji
输出:
31