Related Articles

# Find bitwise AND (&) of all possible sub-arrays

• Difficulty Level : Medium
• Last Updated : 19 May, 2021

Given an array A of size N where . The task is to find the AND of all possible sub-arrays of A and then the AND of all these results.

Examples:

Input : 1 2 3
Output : 0
All possible subarrays are
{1}, {2}, {3}, {1, 2}, {2, 3} and {1, 2, 3}
ANDs of these subarrays are 1, 2, 3, 0, 2, 0.
AND of these ANDs is 0.

Input : 100 500 1000
Output : 96 

Approach:
The Naive solution is to find the AND of all the sub-arrays and then print the AND of their results. This will lead to O(N2) solution.

Optimal Solution: Using the properties of i:e it doesn’t matter how many times an element comes, its ANDing will be counted as one only. Thus, our problem is reduced to finding the AND of all the elements of the array only.

Below is the implementation of the above approach.

## C++

 // C++ program to find of all the sub-arrays#include using namespace std; // function to return AND of sub-arraysint AND(int a[], int n){    int ans = a;    for (int i = 0; i < n; ++i)        ans &= a[i];     return ans;} // Driver programint main(){    int a[] = { 1, 2, 3 };     // size of the array    int n = sizeof(a) / sizeof(a);     // print and of all subarrays    cout << AND(a, n);     return 0;}

## Java

 //Java program to find of all the sub-arrayspublic class GFG {     //function to return AND of sub-arrays    static int AND(int a[], int n)    {     int ans = a;     for (int i = 0; i < n; ++i)         ans &= a[i];      return ans;    }     // Driver code    public static void main(String[] args) {             int a[] = { 1, 2, 3 };          // size of the array         int n = a.length;          // print and of all subarrays         System.out.println(AND(a, n));    }}

## Python 3

 # Python 3 Program  to find of all the sub-arrays # function to return AND of sub-arraysdef AND(a, n) :     ans = a    for i in range(n) :        ans &= a[i]             return ans   # Driver Codeif __name__ == "__main__" :     a = [ 1, 2, 3]     # size of the array    n = len(a)     # print and of all subarrays    print(AND(a, n)) # This code is contributed by ANKITRAI1

## C#

 //C# program to find of all the sub-arrays  using System; public class GFG {       //function to return AND of sub-arrays    static int AND(int []a, int n)    {     int ans = a;     for (int i = 0; i < n; ++i)          ans &= a[i];       return ans;    }       // Driver code    public static void Main() {               int []a = { 1, 2, 3 };            // size of the array         int n = a.Length;            // print and of all subarrays         Console.WriteLine(AND(a, n));    }}

## PHP

 

## Javascript

 

OUTPUT:

0

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up