📜  不使用OR运算符的阵列中一对的最大OR值

📅  最后修改于: 2021-05-17 06:10:37             🧑  作者: Mango

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

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

  • 在数组中找出最大元素,然后在剩余数组中找到将在最大元素具有未设置位的索引处具有设置位的特定元素。
  • 为了最大化我们的输出,我们必须找到这样一个元素,该元素将以最大化我们的输出的方式具有固定的位。
  • 计算数组中最大元素的补数,并找到与其他数字的最大AND值。
  • 此补语与其他数组元素的最大AND值将为我们提供由于其他数组元素而可能在我们的答案中设置的最大未设置位。
  • 在不使用OR运算的情况下,将具有此最大AND值的最大元素相加即可得到我们所需的最大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


输出:
24

时间复杂度: O(N)