📜  数组的所有子数组的按位与的按位或

📅  最后修改于: 2021-09-07 02:11:51             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是找到给定数组的所有子数组的按位与的按位或。

例子:

朴素方法:解决给定问题的最简单方法是生成给定数组的所有可能子数组,然后找到所有生成的子数组的所有按位与的按位或作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the Bitwise OR
// of Bitwise AND of all subarrays
void findbitwiseOR(int* a, int n)
{
    // Stores the required result
    int res = 0;
 
    // Generate all the subarrays
    for (int i = 0; i < n; i++) {
 
        // Store the current element
        int curr_sub_array = a[i];
 
        // Find the Bitwise OR
        res = res | curr_sub_array;
 
        for (int j = i; j < n; j++) {
 
            // Update the result
            curr_sub_array = curr_sub_array
                             & a[j];
            res = res | curr_sub_array;
        }
    }
 
    // Print the result
    cout << res;
}
 
// Driver Code
int main()
{
    int A[] = { 1, 2, 3 };
    int N = sizeof(A) / sizeof(A[0]);
    findbitwiseOR(A, N);
 
    return 0;
}


Java
// Java program for the above approach
public class GFG {
 
    // Function to find the Bitwise OR
    // of Bitwise AND of all subarrays
    static void findbitwiseOR(int[] a, int n)
    {
        // Stores the required result
        int res = 0;
 
        // Generate all the subarrays
        for (int i = 0; i < n; i++) {
 
            // Store the current element
            int curr_sub_array = a[i];
 
            // Find the Bitwise OR
            res = res | curr_sub_array;
 
            for (int j = i; j < n; j++) {
 
                // Update the result
                curr_sub_array = curr_sub_array & a[j];
                res = res | curr_sub_array;
            }
        }
 
        // Print the result
        System.out.println(res);
    }
    // Driver code
    public static void main(String[] args)
    {
        int A[] = { 1, 2, 3 };
        int N = A.length;
        findbitwiseOR(A, N);
    }
}
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to find the Bitwise OR
# of Bitwise AND of all subarrays
def findbitwiseOR(a, n):
     
    # Stores the required result
    res = 0
 
    # Generate all the subarrays
    for i in range(n):
         
        # Store the current element
        curr_sub_array = a[i]
 
        # Find the Bitwise OR
        res = res | curr_sub_array
 
        for j in range(i, n):
             
            # Update the result
            curr_sub_array = curr_sub_array & a[j]
            res = res | curr_sub_array
 
    # Print the result
    print (res)
 
# Driver Code
if __name__ == '__main__':
     
    A = [ 1, 2, 3 ]
    N = len(A)
     
    findbitwiseOR(A, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
     
    // Function to find the Bitwise OR
    // of Bitwise AND of all subarrays
    static void findbitwiseOR(int[] a, int n)
    {
        // Stores the required result
        int res = 0;
  
        // Generate all the subarrays
        for (int i = 0; i < n; i++) {
  
            // Store the current element
            int curr_sub_array = a[i];
  
            // Find the Bitwise OR
            res = res | curr_sub_array;
  
            for (int j = i; j < n; j++) {
  
                // Update the result
                curr_sub_array = curr_sub_array & a[j];
                res = res | curr_sub_array;
            }
        }
  
        // Print the result
        Console.Write(res);
    }
 
// Driver code
static void Main()
{
    int[] A = { 1, 2, 3 };
        int N = A.Length;
        findbitwiseOR(A, N);
 
}
}
 
// This code is contributed by sanjoy_62.


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the Bitwise OR of
// Bitwise AND of all consecutive
// subsets of the array
void findbitwiseOR(int* a, int n)
{
    // Stores the required result
    int res = 0;
 
    // Traverse the given array
    for (int i = 0; i < n; i++)
        res = res | a[i];
 
    // Print the result
    cout << res;
}
 
// Driver Code
int main()
{
    int A[] = { 1, 2, 3 };
    int N = sizeof(A) / sizeof(A[0]);
    findbitwiseOR(A, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
   
// Function to find the Bitwise OR of
// Bitwise AND of all consecutive
// subsets of the array
static void findbitwiseOR(int[] a, int n)
{
     
    // Stores the required result
    int res = 0;
 
    // Traverse the given array
    for(int i = 0; i < n; i++)
        res = res | a[i];
 
    // Print the result
    System.out.println(res);
}
 
// Driver Code
public static void main(String[] args)
{
    int[] A = { 1, 2, 3 };
    int N = A.length;
     
    findbitwiseOR(A, N);
}
}
 
// This code is contributed by Dharanendra L V.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the Bitwise OR of
// Bitwise AND of all consecutive
// subsets of the array
static void findbitwiseOR(int[] a, int n)
{
     
    // Stores the required result
    int res = 0;
 
    // Traverse the given array
    for(int i = 0; i < n; i++)
        res = res | a[i];
 
    // Print the result
    Console.Write(res);
}
 
// Driver Code
public static void Main()
{
    int[] A = { 1, 2, 3 };
    int N = A.Length;
     
    findbitwiseOR(A, N);
}
}
 
// This code is contributed by ukasp


Javascript


输出:

3

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

高效方法:上述方法也可以基于观察到任何子数组的按位与总是小于或等于子数组中的第一个元素来优化。因此,最大可能值是子数组的按位 AND 是元素本身。因此,任务被简化为找到所有数组元素的按位或作为结果。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the Bitwise OR of
// Bitwise AND of all consecutive
// subsets of the array
void findbitwiseOR(int* a, int n)
{
    // Stores the required result
    int res = 0;
 
    // Traverse the given array
    for (int i = 0; i < n; i++)
        res = res | a[i];
 
    // Print the result
    cout << res;
}
 
// Driver Code
int main()
{
    int A[] = { 1, 2, 3 };
    int N = sizeof(A) / sizeof(A[0]);
    findbitwiseOR(A, N);
 
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
 
class GFG{
   
// Function to find the Bitwise OR of
// Bitwise AND of all consecutive
// subsets of the array
static void findbitwiseOR(int[] a, int n)
{
     
    // Stores the required result
    int res = 0;
 
    // Traverse the given array
    for(int i = 0; i < n; i++)
        res = res | a[i];
 
    // Print the result
    System.out.println(res);
}
 
// Driver Code
public static void main(String[] args)
{
    int[] A = { 1, 2, 3 };
    int N = A.length;
     
    findbitwiseOR(A, N);
}
}
 
// This code is contributed by Dharanendra L V.

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the Bitwise OR of
// Bitwise AND of all consecutive
// subsets of the array
static void findbitwiseOR(int[] a, int n)
{
     
    // Stores the required result
    int res = 0;
 
    // Traverse the given array
    for(int i = 0; i < n; i++)
        res = res | a[i];
 
    // Print the result
    Console.Write(res);
}
 
// Driver Code
public static void Main()
{
    int[] A = { 1, 2, 3 };
    int N = A.Length;
     
    findbitwiseOR(A, N);
}
}
 
// This code is contributed by ukasp

Javascript


输出:
3

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

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