📜  不使用 OR运算符的数组中一对的最大 OR 值

📅  最后修改于: 2021-09-06 11:29:00             🧑  作者: Mango

给定一个包含N 个正整数的数组arr[] ,任务是在不使用按位 OR运算符的情况下找到给定数组中一对的最大按位 OR 值。
例子:

方法:这个想法是找到在不同索引处具有最多设置位计数的两个数字。通过这种方式,结果数字将所有这些索引作为一个设置位,这可以在不使用 OR运算符的情况下完成。

  • 找出数组中的最大元素,然后在剩余数组中找到将在最大元素具有未设置位的索引处设置位的特定元素。
  • 为了最大化我们的输出,我们必须找到这样一个元素,该元素将以最大化我们的输出的方式设置位。
  • 计算数组中最大元素的补码,并找到与其他数字的最大 AND 值。
  • 此恭维与其他数组元素的最大 AND 值将为我们提供由于其他数组元素而可以在我们的答案中设置的最大未设置位。
  • 添加具有此最大 AND 值的最大元素将为我们提供所需的最大 OR 值对,而无需使用 OR 运算。

下面是上述方法的实现:

C++
// C++ implementation of the approach
 
#include 
using namespace std;
 
// Function to return the maximum bitwise OR
// for any pair of the given array
// without using bitwise OR operation
int maxOR(int arr[], int n)
{
 
    // find maximum element in the array
    int max_value
        = *max_element(arr, arr + n);
 
    int number_of_bits
        = floor(log2(max_value)) + 1;
 
    // finding compliment will set
    // all unset bits in a number
    int compliment
        = ((1 << number_of_bits) - 1)
          ^ max_value;
 
    int c = 0;
 
    // iterate through all other
    // array elements to find
    // maximum AND value
    for (int i = 0; i < n; i++) {
        if (arr[i] != max_value) {
            c = max(c, (compliment & arr[i]));
        }
    }
 
    // c will give the maximum value
    // that could be added to max_value
    // to produce maximum OR value
    return (max_value + c);
}
 
// Driver code
int main()
{
    int arr[] = { 3, 6, 8, 16 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << maxOR(arr, n);
 
    return 0;
}


Java
// Java implementation
// of the approach
import java.util.*;
class GFG{
 
// Function to return the maximum
// bitwise OR for any pair of the
// given array without using bitwise
// OR operation
static int maxOR(int arr[], int n)
{
 
    // find maximum element in the array
    int max_value =
        Arrays.stream(arr).max().getAsInt();
 
    int number_of_bits =
        (int)((Math.log(max_value))) + 2;
 
    // finding compliment will set
    // all unset bits in a number
    int compliment = ((1 << number_of_bits) - 1) ^
                       max_value;
 
    int c = 0;
 
    // iterate through all other
    // array elements to find
    // maximum AND value
    for (int i = 0; i < n; i++)
    {
        if (arr[i] != max_value)
        {
            c = Math.max(c,
                         (compliment & arr[i]));
        }
    }
 
    // c will give the maximum value
    // that could be added to max_value
    // to produce maximum OR value
    return (max_value + c);
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = {3, 6, 8, 16};
    int n = arr.length;
    System.out.print(maxOR(arr, n));
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 implementation of the approach
from math import log2, floor
 
# Function to return the maximum bitwise OR
# for any pair of the given array
# without using bitwise OR operation
def maxOR(arr, n):
     
    # Find maximum element in the array
    max_value = max(arr)
 
    number_of_bits = floor(log2(max_value)) + 1
 
    # Finding compliment will set
    # all unset bits in a number
    compliment = (((1 << number_of_bits) - 1) ^
                 max_value)
    c = 0
 
    # Iterate through all other
    # array elements to find
    # maximum AND value
    for i in range(n):
         
        if (arr[i] != max_value):
            c = max(c, (compliment & arr[i]))
 
    # c will give the maximum value
    # that could be added to max_value
    # to produce maximum OR value
    return (max_value + c)
 
# Driver code
if __name__ == '__main__':
     
    arr = [3, 6, 8, 16]
    n = len(arr)
 
    print(maxOR(arr, n))
 
# This code is contributed by Bhupendra_Singh


C#
// C# program for the above approach
using System;
using System.Linq;
 
class GFG{
 
// Function to return the maximum
// bitwise OR for any pair of the
// given array without using bitwise
// OR operation
static int maxOR(int[] arr, int n)
{
 
    // Find maximum element in the array
    int max_value = arr.Max();
 
    int number_of_bits = (int)(Math.Log(max_value)) + 2;
 
    // Finding compliment will set
    // all unset bits in a number
    int compliment = ((1 << number_of_bits) - 1) ^
                      max_value;
 
    int c = 0;
 
    // Iterate through all other
    // array elements to find
    // maximum AND value
    for(int i = 0; i < n; i++)
    {
        if (arr[i] != max_value)
        {
            c = Math.Max(c,
                        (compliment & arr[i]));
        }
    }
 
    // c will give the maximum value
    // that could be added to max_value
    // to produce maximum OR value
    return (max_value + c);
}
 
// Driver code
public static void Main()
{
    int[] arr = { 3, 6, 8, 16 };
    int n = arr.Length;
     
    Console.Write(maxOR(arr, n));
}
}
 
// This code is contributed by code_hunt


Javascript


输出:

24

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live