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

Given an array A of size N where, 1\leq N \leq 10^{5}. 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 property that X\&X\&...\&X=X i:e it doesn’t matter how many times an element comes, it’s ANDing will be counted as one only. Thus our problem reduces to finding the AND of all the elements of the array only.
Below is the implementation of above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//Java program to find of all the sub-arrays
public class GFG {
  
    //function to return AND of sub-arrays
    static int AND(int a[], int n)
    {
     int ans = a[0];
     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));
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 Program  to find of all the sub-arrays 
  
# function to return AND of sub-arrays
def AND(a, n) :
  
    ans = a[0]
    for i in range(n) :
        ans &= a[i]
          
    return ans
  
  
  
# Driver Code
if __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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

//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[0]; 
     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)); 
    

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find of 
// all the sub-arrays 
  
// function to return AND 
// of sub-arrays 
function ANDS(&$a, $n
    $ans = $a[0]; 
    for ($i = 0; $i < $n; ++$i
        $ans &= $a[$i]; 
    return $ans
  
// Driver Code
$a = array( 1, 2, 3 ); 
  
// size of the array 
$n = sizeof($a); 
  
// print and of all subarrays 
echo ANDS($a, $n); 
  
// This code is contributed
// by Shivi_Aggarwal 
?>

chevron_right



OUTPUT:

0

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.