Largest set with bitwise OR equal to n
Last Updated :
17 Apr, 2023
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 between 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 to 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.
1. declare a vector v of integer elements.
2. iterate through i=0 till n:
*check if i bitwise or n is equal to n than push i in v.
3. iterate through i=0 till size of v:
*print v[i].
C++
// 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;
}
Java
// 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 */
Python3
# 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
C#
// 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
JavaScript
<script>
// JavaScript Program to find the largest set
// with bitwise OR equal to n
// function to find the largest set with
// bitwise OR equal to n
function setBitwiseORk(n)
{
var v = [];
for (var 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(i);
}
for (var i = 0; i < v.length; i++)
document.write( v[i] + ' ');
}
// Driver Code
var n = 5;
setBitwiseORk(n);
</script>
Output:
0 1 4 5
Time complexity: O(N)
Auxiliary Space:O(N)
Similar Reads
Maximum subset with bitwise OR equal to k Given an array of non-negative integers and an integer k, find the subset of maximum length with bitwise OR equal to k. Examples: Input : arr[] = [1, 4, 2] k = 3 Output : [1, 2] Explanation: The bitwise OR of 1 and 2 equals 3. It is not possible to obtain a subset of length greater than 2. Input : a
8 min read
Find the size of Largest Subset with positive Bitwise AND Given an array arr[] consisting of N positive integers, the task is to find the largest size of the subset of the array arr[] with positive Bitwise AND. Note : If there exist more than one such subsets then return size of only one subset. Examples: Input: arr[] = [7, 13, 8, 2, 3]Output: 3Explanation
6 min read
Find the largest number with n set and m unset bits Given two non-negative numbers n and m. The problem is to find the largest number having n number of set bits and m number of unset bits in its binary representation.Note : 0 bits before leading 1 (or leftmost 1) in binary representation are countedConstraints: 1 <= n, 0 <= m, (m+n) <= 31Ex
6 min read
Largest number less than equal to N having exactly K set bits Given two positive integers N and K, we need to determine if there exists a positive integer X less than or equal to N such that the number of 1s in the binary representation of X is equal to k (i.e., f(X) = K). If such an X exists, we also need to find the maximum value of X and print -1 if there i
13 min read
Check whether K-th bit is set or not Given a number n and a bit position k, check if the kth bit of n is set or not. A bit is called set if it is 1. Note: Indexing starts with 0 from LSB (least significant bit) side in the binary representation of the number.Examples: Input: n = 7, k = 2Output: YesExplanation: 7 is represented as 111 i
6 min read
Size of the smallest subset with maximum Bitwise OR Given an array of positive integers. The task is to find the size of the smallest subset such that the Bitwise OR of that set is Maximum possible. Examples: Input : arr[] = {5, 1, 3, 4, 2}Output : 2Explanation: 7 is the maximum value possible of OR, 5|2 = 7 and 5|3 = 7 Input : arr[] = {2, 6, 2, 8, 4
15+ min read
Find N distinct numbers whose bitwise Or is equal to K Given two integers N and K, the task is to find N distinct integers whose bit-wise OR is equal to K. If there does not exist any possible answer then print -1. Examples: Input: N = 3, K = 5 Output: 5 0 1 5 OR 0 OR 1 = 5Input: N = 10, K = 5 Output: -1 It is not possible to find any solution. Approach
10 min read
Largest number less than X having at most K set bits Given an integer X > 1 and an integer K > 0, the task is to find the greatest odd number < X such that the number of 1's in its binary representation is at most K. Examples: Input: X = 10, K = 2 Output: 10 Input: X = 29, K = 2 Output: 24 Naive Approach: Starting from X - 1 check all the num
4 min read
Find size of largest subset with bitwise AND greater than their bitwise XOR Given an array arr[] of N integers, the task is to find the size of the largest subset such that the bitwise AND of all elements of the subset is greater than the bitwise XOR of all elements of the subset. Example: Input: arr[] = {1, 2, 3, 4, 5}Output: 2Explanation: The subset {2, 3} has the bitwise
6 min read
Maximum steps to transform 0 to X with bitwise AND For an integer N, there are elements ranging from 0 to N-1. Certain elements can be transformed into other elements. Each transformation requires a certain effort which is equal to 1 unit, for each transformation. An element A can be transformed to element B, if and only if A != B and A & B = A
5 min read