📜  数组中一对的最大或值

📅  最后修改于: 2022-05-13 01:57:49.954000             🧑  作者: Mango

数组中一对的最大或值

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

方法:遍历所有可能的对并计算这些对的 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
int maxOR(int arr[], int n)
{
 
    // To store the maximum OR value
    int maxVal = 0;
 
    // For every possible pair
    for (int i = 0; i < n - 1; i++)
        for (int j = i + 1; j < n; j++) {
 
            // Update the maximum OR value
            maxVal = max(maxVal, arr[i] | arr[j]);
        }
 
    return maxVal;
}
 
// Driver code
int main()
{
    int arr[] = { 4, 8, 12, 16 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << maxOR(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to return the maximum bitwise OR
    // for any pair of the given array
    static int maxOR(int arr[], int n)
    {
 
        // To store the maximum OR value
        int maxVal = 0;
 
        // For every possible pair
        for (int i = 0; i < n - 1; i++)
            for (int j = i + 1; j < n; j++) {
 
                // Update the maximum OR value
                maxVal = Math.max(maxVal, arr[i] | arr[j]);
            }
 
        return maxVal;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 4, 8, 12, 16 };
        int n = arr.length;
 
        System.out.println(maxOR(arr, n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
 
# Function to return the maximum bitwise OR
# for any pair of the given array
def maxOR(arr, n):
     
    # To store the maximum OR value
    maxVal = 0;
 
    # For every possible pair
    for i in range(n - 1):
        for j in range(i + 1, n):
             
            # Update the maximum OR value
            maxVal = max(maxVal, arr[i] | arr[j]);
 
    return maxVal;
 
# Driver code
if __name__ == '__main__':
    arr = [4, 8, 12, 16];
    n = len(arr);
 
    print(maxOR(arr, n));
 
# This code is contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
 
class GFG {
 
    // Function to return the maximum bitwise OR
    // for any pair of the given array
    static int maxOR(int[] arr, int n)
    {
 
        // To store the maximum OR value
        int maxVal = 0;
 
        // For every possible pair
        for (int i = 0; i < n - 1; i++)
            for (int j = i + 1; j < n; j++) {
 
                // Update the maximum OR value
                maxVal = Math.Max(maxVal, arr[i] | arr[j]);
            }
 
        return maxVal;
    }
 
    // Driver code
    static public void Main()
    {
        int[] arr = { 4, 8, 12, 16 };
        int n = arr.Length;
 
        Console.Write(maxOR(arr, n));
    }
}
 
// This code is contributed by ajit.


Javascript


C++
// C++ implementation of the approach ( Linear time Complexity )
 
#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 maximum element in the array
    int max_value = *max_element(arr, arr + n);
 
    // To store the maximum OR value
    int ans = 0;
 
    // iterate over rest array elements and find maximum OR value pair
    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
// (Linear time Complexity)
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.
public static int maxOR(int[] arr, int n)
{
     
    // Find maximum element in the array
    int max_value = Arrays.stream(arr).max().getAsInt();
     
    // To store the maximum OR value
    int ans = 0;
   
    // Iterate over rest array elements and
    // find maximum OR value pair
    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 = arr.length;
     
    System.out.print(maxOR(arr, n));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation of the approach
# (Linear time Complexity)
  
# Function to return the maximum bitwise
# OR for any pair of the given array
def maxOR(arr, n):
      
    # To store the maximum element
    # of the array
    maxVal = max(arr)
     
    # To store the maximum OR value
    ans = 0
     
    # Iterate over rest array elements and
    # find maximum OR value pair
    for i in range(n - 1):
         
        # Update the maximum OR value
        ans = max(ans, maxVal | 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 math_lover


C#
// C# implementation of the approach
// (Linear time Complexity)
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 maximum element in the array
    int max_value = arr.Max();
 
    // To store the maximum OR value
    int ans = 0;
 
    // Iterate over rest array elements and
    // find maximum OR value pair
    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 = arr.Length;
 
    Console.Write(maxOR(arr, n));
}
}
 
// This code is contributed by doreamon_


Javascript


输出:
28

有效方法:因为我们必须找到这样一个能够给出最大 OR 值的对,即,我们的输出必须具有设置位,以便它将形成最大 OR 值对。为此,我们将在数组中找到最大元素,因为它设置了对最大值有贡献的位排列,因此我们只需要找到最大数组元素的最大 OR 值,因为必须有一对给出最大 OR 值,其中一个元素是最大数组元素。

C++

// C++ implementation of the approach ( Linear time Complexity )
 
#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 maximum element in the array
    int max_value = *max_element(arr, arr + n);
 
    // To store the maximum OR value
    int ans = 0;
 
    // iterate over rest array elements and find maximum OR value pair
    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
// (Linear time Complexity)
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.
public static int maxOR(int[] arr, int n)
{
     
    // Find maximum element in the array
    int max_value = Arrays.stream(arr).max().getAsInt();
     
    // To store the maximum OR value
    int ans = 0;
   
    // Iterate over rest array elements and
    // find maximum OR value pair
    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 = arr.length;
     
    System.out.print(maxOR(arr, n));
}
}
 
// This code is contributed by divyeshrabadiya07

Python3

# Python3 implementation of the approach
# (Linear time Complexity)
  
# Function to return the maximum bitwise
# OR for any pair of the given array
def maxOR(arr, n):
      
    # To store the maximum element
    # of the array
    maxVal = max(arr)
     
    # To store the maximum OR value
    ans = 0
     
    # Iterate over rest array elements and
    # find maximum OR value pair
    for i in range(n - 1):
         
        # Update the maximum OR value
        ans = max(ans, maxVal | 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 math_lover

C#

// C# implementation of the approach
// (Linear time Complexity)
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 maximum element in the array
    int max_value = arr.Max();
 
    // To store the maximum OR value
    int ans = 0;
 
    // Iterate over rest array elements and
    // find maximum OR value pair
    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 = arr.Length;
 
    Console.Write(maxOR(arr, n));
}
}
 
// This code is contributed by doreamon_

Javascript


输出:

24

时间复杂度: O(n)