📜  数组中一对的最大 OR 值 | 2套

📅  最后修改于: 2021-09-03 04:17:13             🧑  作者: Mango

给定一个包含N 个正元素的数组arr[] ,任务是从给定数组中找到一对的最大按位 OR 值。
例子:

朴素方法:请参阅数组中一对的最大 OR 值以了解朴素方法。
高效的方法:在本文中,我们将讨论针对给定问题的优化解决方案。
请按照以下步骤解决问题:

  • 从数组中找出最大的元素。
  • 在最大元素和剩余数组元素之间一一执行按位或。这是因为最大元素中设置位的排列将有助于从数组元素中获得最大可能的 OR 值。
  • 打印执行上述步骤获得的最大 OR 值。

下面是上述方法的实现:

C++
// C++ implementation of
// the approach above
 
#include 
using namespace std;
 
// Function to return the maximum
// bitwise OR for any pair of the
// given array in O(n) time complexity.
int maxOR(int arr[], int n)
{
    // Find the maximum
    // element in the array
    int max_value
        = *max_element(arr,
                       arr + n);
 
    // Stores the maximum
    // OR value
    int ans = 0;
 
    // Traverse the array and
    // perform Bitwise OR
    // between every array element
    // with the maximum element
    for (int i = 0; i < n; i++) {
        ans = max(ans, (max_value
                        | arr[i]));
    }
    return ans;
}
 
// 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 above
import java.util.Arrays;
class GFG{
     
// Function to return the maximum
// bitwise OR for any pair of the
// given array in O(n) time complexity.
static int maxOR(int []arr, int n)
{
    // Find the maximum
    // element in the array
    int max_value = Arrays.stream(arr).max().getAsInt();
 
    // Stores the maximum
    // OR value
    int ans = 0;
 
    // Traverse the array and
    // perform Bitwise OR
    // between every array element
    // with the maximum element
    for (int i = 0; i < n; i++)
    {
        ans = Math.max(ans, (max_value | arr[i]));
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = new int[]{ 3, 6, 8, 16 };
    int n = 4;
 
    System.out.print(maxOR(arr, n));
}
}
 
// This code is contributed by Ritik Bansal


Python3
# Python3 implementation of
# the approach above
 
# Function to return the maximum
# bitwise OR for any pair of the
# given array in O(n) time complexity.
def maxOR(arr, n):
 
    # Find the maximum
    # element in
    max_value = max(arr)
     
    # Stores the maximum
    # OR value the array
    ans = 0
 
    # Traverse the array and
    # perform Bitwise OR
    # between every array element
    # with the maximum element
    for i in range(n):
        ans = max(ans, (max_value | arr[i]))
     
    return ans
 
# Driver Code
if __name__ == "__main__":
     
    arr = [ 3, 6, 8, 16 ]
    n = len(arr)
 
    print(maxOR(arr, n))
 
# This code is contributed by jana_sayantan


C#
// C# implementation of
// the approach above
using System;
using System.Linq;
 
class GFG{
     
// Function to return the maximum
// bitwise OR for any pair of the
// given array in O(n) time complexity.
static int maxOR(int []arr, int n)
{
     
    // Find the maximum
    // element in the array
    int max_value = arr.Max();
 
    // Stores the maximum
    // OR value
    int ans = 0;
 
    // Traverse the array and
    // perform Bitwise OR
    // between every array element
    // with the maximum element
    for(int i = 0; i < n; i++)
    {
        ans = Math.Max(ans, (max_value |
                             arr[i]));
    }
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 3, 6, 8, 16 };
    int n = 4;
 
    Console.Write(maxOR(arr, n));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:

24

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

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