Given an integer n, find the largest possible set of non negative integers with bitwise OR equal to n.

**Examples:**

Input : n = 5 Output : arr[] = [0, 1, 4, 5] The bitwise OR of 0, 1, 4 and 5 equals 5. It is not possible to obtain a set larger than this. Input : n = 8 Output : arr[] = [0, 8]

**Prerequisite:** Maximum subset with bitwise OR equal to k

The difference in the above referenced article and this post is the number of elements to be checked. In the above referenced article, we have an array of n numbers and in this post, we have the entire set of non negative numbers.

Traversing an array was simple with the time complexity of O(N), but traversing the boundless set of non negative numbers is not possible. So how do we limit ourselves to a smaller set of numbers?

The answer lies in the concept used. For any number, x greater than n, the bitwise OR of x and n will never be equal to n.

Hence we only need to traverse from 0 to n to obtain our answer.

The second difference is that there will always be an answer for this question. On the other hand, there was no certainty in the existence of an answer in the above referenced article. This is because we can always include n in the resulting set.

**Algorithm:**

Traverse the numbers from 0 to n, checking its bitwise OR with n. If the bitwise OR equals n, then include that number in the resulting set.

`// CPP Program to find the largest set ` `// with bitwise OR equal to n ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// function to find the largest set with ` `// bitwise OR equal to n ` `void` `setBitwiseORk(` `int` `n) `
`{ ` ` ` `vector<` `int` `> v; `
` ` ` ` `for` `(` `int` `i = 0; i <= n; i++) { `
` ` ` ` `// If the bitwise OR of n and i `
` ` `// is equal to n, then include i `
` ` `// in the set `
` ` `if` `((i | n) == n) `
` ` `v.push_back(i); `
` ` `} `
` ` ` ` `for` `(` `int` `i = 0; i < v.size(); i++) `
` ` `cout << v[i] << ` `' '` `; `
`} ` ` ` `// Driver Code ` `int` `main() `
`{ ` ` ` `int` `n = 5; `
` ` ` ` `setBitwiseORk(n); `
` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`// Java Program to find the largest set ` `// with bitwise OR equal to n ` `import` `java.util.*; `
` ` `class` `GFG `
`{ ` ` ` ` ` `// function to find the largest set with `
` ` `// bitwise OR equal to n `
` ` `static` `void` `setBitwiseORk(` `int` `n) `
` ` `{ `
` ` `Vector<Integer> v = ` `new` `Vector<Integer>(); `
` ` ` ` `for` `(` `int` `i = ` `0` `; i <= n; i++) `
` ` `{ `
` ` ` ` `// If the bitwise OR of n and i `
` ` `// is equal to n, then include i `
` ` `// in the set `
` ` `if` `((i | n) == n) `
` ` `{ `
` ` `v.add(i); `
` ` `} `
` ` `} `
` ` ` ` `for` `(` `int` `i = ` `0` `; i < v.size(); i++) `
` ` `{ `
` ` `System.out.print(v.get(i) + ` `" "` `); `
` ` `} `
` ` `} `
` ` ` ` `// Driver Code `
` ` `public` `static` `void` `main(String[] args) `
` ` `{ `
` ` `int` `n = ` `5` `; `
` ` ` ` `setBitwiseORk(n); `
` ` `} `
`} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

`# Python 3 Program to find the largest ` `# set with bitwise OR equal to n ` ` ` `# function to find the largest set ` `# with bitwise OR equal to n ` `def` `setBitwiseORk(n): `
` ` `v ` `=` `[] `
` ` ` ` `for` `i ` `in` `range` `(` `0` `, n ` `+` `1` `, ` `1` `): `
` ` ` ` `# If the bitwise OR of n and i `
` ` `# is equal to n, then include i `
` ` `# in the set `
` ` `if` `((i | n) ` `=` `=` `n): `
` ` `v.append(i) `
` ` ` ` `for` `i ` `in` `range` `(` `0` `, ` `len` `(v), ` `1` `): `
` ` `print` `(v[i], end ` `=` `' '` `) `
` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: `
` ` `n ` `=` `5`
` ` ` ` `setBitwiseORk(n) `
` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

`// C# Program to find the largest set ` `// with bitwise OR equal to n ` `using` `System; `
`using` `System.Collections.Generic; `
` ` `class` `GFG `
`{ ` ` ` ` ` `// function to find the largest set with `
` ` `// bitwise OR equal to n `
` ` `static` `void` `setBitwiseORk(` `int` `n) `
` ` `{ `
` ` `List<` `int` `> v = ` `new` `List<` `int` `>(); `
` ` ` ` `for` `(` `int` `i = 0; i <= n; i++) `
` ` `{ `
` ` ` ` `// If the bitwise OR of n and i `
` ` `// is equal to n, then include i `
` ` `// in the set `
` ` `if` `((i | n) == n) `
` ` `{ `
` ` `v.Add(i); `
` ` `} `
` ` `} `
` ` ` ` `for` `(` `int` `i = 0; i < v.Count; i++) `
` ` `{ `
` ` `Console.Write(v[i] + ` `" "` `); `
` ` `} `
` ` `} `
` ` ` ` `// Driver Code `
` ` `public` `static` `void` `Main(String[] args) `
` ` `{ `
` ` `int` `n = 5; `
` ` ` ` `setBitwiseORk(n); `
` ` `} `
`} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

0 1 4 5

**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.

## Recommended Posts:

- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Numbers whose bitwise OR and sum with N are equal
- Maximum subset with bitwise OR equal to k
- Find N distinct numbers whose bitwise Or is equal to K
- Minimum Bitwise OR operations to make any two array elements equal
- Rearrange array elements such that Bitwise AND of first N - 1 elements is equal to last element
- Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Count of pairs from Array with sum equal to twice their bitwise AND
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
- Count even length subarrays having bitwise XOR equal to 0
- Print bitwise AND set of a number N
- Sum of bitwise OR of all possible subsets of given set

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.

**Practice Tags :**