- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Numbers that are Bitwise AND of At Least One Non-Empty Sub-Array using C++

To solve a problem where we are given an array, and we need to find all possible integers which are bitwise AND of at least one not empty subarray, for example −

Input : nums[ ] = { 3, 5, 1, 2, 8 } Output : { 2, 5, 0, 3, 8, 1 } Explanation: 2 is the bitwise AND of subarray {2}, 5 is the bitwise AND of subarray {5}, 0 is the bitwise AND of subarray {1, 2}, {2, 8} and {1, 2, 8}, 3 is the bitwise AND of subarray {3}, 8 is the bitwise AND of subarray {8}, 1 is the bitwise AND of subarray {1}, {3, 5} and {3, 5, 1}. Input : nums[ ] = { 2, 6, 3, 8, 1 } Output: { 1, 8, 3, 6, 2, 0 }

## Approach to Find the Solution

A **Simple Approach** that can be applied is,

Find all the possible non-empty subarrays.

While traversing through the array, calculate the bitwise AND of each element in the subarray.

To avoid duplicate values, store all the results in a set.

## Example

#include <bits/stdc++.h> using namespace std; int main(){ int arr[] ={ 2, 6, 3, 8, 1 }; int n = sizeof(arr) / sizeof(arr[0]); // Declaring set to store result of each AND operation. unordered_set<int> result; int val; // nested loops to traverse through all the possible non empty subarrays. for (int i = 0; i < n; ++i){ for (int j = i, val = INT_MAX; j < n; ++j){ val = val & arr[j]; // storing result of AND operation result.insert(val); } } cout << "All possible numbers are: "; // printing all the values of set. for (auto i = result.begin(); i != result.end();i++) cout << *i << " "; return 0; }

## Output

All possible numbers are: 1 8 3 6 0 2

## Explanation of the Above Code

Declaring set to store all the results of AND operation.

Initializing the ‘val’ variable with INT_MAX because we need to do AND operation with all the bits set to 1.

Inside loop to go through all possible subarrays from the ith index.

Calculating AND operation of each element with each other and with themselves and storing in the result set.

Printing all the values of the result set.

## Conclusion

In this tutorial, We discussed a simple approach to solve this problem, i.e., calculating AND operation in every possible subarray. We also discussed the C++ program to solve this problem. Also, you can write this code in any other language like Java, C, Python, etc. We hope you find this tutorial helpful.

- Related Articles
- Print array elements that are divisible by at-least one other in C++
- Bitwise AND of sub-array closest to K in C++
- Count elements that are divisible by at-least one element in another array in C++
- Count of sub-strings that contain character X at least once in C++
- How to extract columns having at least one non-duplicate in R?
- Count pairs in an array such that at least one element is prime in C++
- Count pairs in an array such that frequency of one is at least value of other in C++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Three coins are tossed together. Find the probability of getting at least one head and one tail.
- How are entire non-empty directory trees removed using Python?
- Find Array Elements Which has at Least One Smaller Element in Java
- How to remove rows that contain at least one 0 in R?
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times in C++
- Count divisors of n that have at-least one digit common with n in Java
- Bitwise AND of Numbers Range in C++