Find N distinct numbers whose bitwise Or is equal to K
Last Updated :
14 Sep, 2022
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 = 5
Input: N = 10, K = 5
Output: -1
It is not possible to find any solution.
Approach:
- We know that if bit-wise OR of a sequence of numbers is K then all the bit positions which are 0 in K must also be zero in all the numbers.
- So, we only have those positions to change where bit is 1 in K. Say that count is Bit_K.
- Now, we can form pow(2, Bit_K) distinct numbers with Bit_K bits. So if, we set one number to be K itself, then rest N - 1 numbers can be built by setting 0 all the bits in every number which are 0 in K and for other bit positions any permutation of Bit_K bits other than number K.
- If pow(2, Bit_K) < N then we cannot find any possible answer.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MAX 32
ll pow2[MAX];
bool visited[MAX];
vector<int> ans;
// Function to pre-calculate
// all the powers of 2 upto MAX
void power_2()
{
ll ans = 1;
for (int i = 0; i < MAX; i++) {
pow2[i] = ans;
ans *= 2;
}
}
// Function to return the
// count of set bits in x
int countAndCheckSetBits(ll x)
{
// To store the count
// of set bits
int setBits = 0;
for (int i = 0; i < MAX; i++) {
int a = (x & (1 << i));
if (a != 0) {
// mark position of set bits true
visited[i] = true;
// increment setBits count
setBits++;
}
else {
visited[i] = false;
}
}
return setBits;
}
// Function to add num to the answer
// by setting all bit positions as 0
// which are also 0 in K
void add(ll num)
{
int point = 0;
ll value = 0;
for (ll i = 0; i < MAX; i++) {
// if Bit i is 1 in K
if (visited[i]) {
if (num & 1) {
value += (1 << i);
}
num /= 2;
}
else {
continue;
}
}
ans.push_back(value);
}
// Function to find and print N distinct
// numbers whose bitwise OR is K
void solve(ll n, ll k)
{
// Choosing K itself as one number
ans.push_back(k);
// Find the count of set bits in K
int countk = countAndCheckSetBits(k);
// Impossible to get N
// distinct integers
if (pow2[countk] < n) {
cout << -1;
return;
}
int count = 0;
for (ll i = 0; i < pow2[countk] - 1; i++) {
// Add i to the answer after
// setting all the bits as 0
// which are 0 in K and only
// keeping all the bits 1
// which are 1 in K
add(i);
count++;
// If N distinct numbers are generated
if (count == n)
break;
}
// Print the generated numbers
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
}
// Driver code
int main()
{
ll n = 4, k = 20;
// Pre-calculate all
// the powers of 2
power_2();
solve(n, k);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
static final int MAX = 32;
static long []pow2 = new long[MAX];
static boolean []visited = new boolean[MAX];
static Vector<Long> ans = new Vector<>();
// Function to pre-calculate
// all the powers of 2 upto MAX
static void power_2()
{
long ans = 1;
for (int i = 0; i < MAX; i++)
{
pow2[i] = ans;
ans *= 2;
}
}
// Function to return the
// count of set bits in x
static int countSetBits(long x)
{
// To store the count
// of set bits
int setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
// Function to add num to the answer
// by setting all bit positions as 0
// which are also 0 in K
static void add(long num)
{
int point = 0;
long value = 0;
for (int i = 0; i < MAX; i++)
{
// Bit i is 0 in K
if (visited[i])
continue;
else
{
if (num %2== 1)
{
value += (1 << i);
}
num /= 2;
}
}
ans.add(value);
}
// Function to find and print N distinct
// numbers whose bitwise OR is K
static void solve(long n, long k)
{
// Choosing K itself as one number
ans.add(k);
// Find the count of set bits in K
int countk = countSetBits(k);
// Impossible to get N
// distinct integers
if (pow2[countk] < n)
{
System.out.print(-1);
return;
}
int count = 0;
for (long i = 0; i < pow2[countk] - 1; i++)
{
// Add i to the answer after
// setting all the bits as 0
// which are 0 in K
add(i);
count++;
// If N distinct numbers are generated
if (count == n)
break;
}
// Print the generated numbers
for (int i = 0; i < n; i++)
{
System.out.print(ans.get(i)+" ");
}
}
// Driver code
public static void main(String[] args)
{
long n = 3, k = 5;
// Pre-calculate all
// the powers of 2
power_2();
solve(n, k);
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python 3 implementation of the approach
MAX = 32
pow2 = [0 for i in range(MAX)]
visited = [False for i in range(MAX)]
ans = []
# Function to pre-calculate
# all the powers of 2 upto MAX
def power_2():
an = 1
for i in range(MAX):
pow2[i] = an
an *= 2
# Function to return the
# count of set bits in x
def countSetBits(x):
# To store the count
# of set bits
setBits = 0
while (x != 0):
x = x & (x - 1)
setBits += 1
return setBits
# Function to add num to the answer
# by setting all bit positions as 0
# which are also 0 in K
def add(num):
point = 0
value = 0
for i in range(MAX):
# Bit i is 0 in K
if (visited[i]):
continue
else:
if (num & 1):
value += (1 << i)
num = num//2
ans.append(value)
# Function to find and print N distinct
# numbers whose bitwise OR is K
def solve(n, k):
# Choosing K itself as one number
ans.append(k)
# Find the count of set bits in K
countk = countSetBits(k)
# Impossible to get N
# distinct integers
if (pow2[countk] < n):
print(-1)
return
count = 0
for i in range(pow2[countk] - 1):
# Add i to the answer after
# setting all the bits as 0
# which are 0 in K
add(i)
count += 1
# If N distinct numbers are generated
if (count == n):
break
# Print the generated numbers
for i in range(n):
print(ans[i],end = " ")
# Driver code
if __name__ == '__main__':
n = 3
k = 5
# Pre-calculate all
# the powers of 2
power_2()
solve(n, k)
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
static int MAX = 32;
static long [] pow2 = new long[MAX];
static bool [] visited = new bool[MAX];
static List<long> ans = new List<long>();
// Function to pre-calculate
// all the powers of 2 upto MAX
static void power_2()
{
long ans = 1;
for (int i = 0; i < MAX; i++)
{
pow2[i] = ans;
ans *= 2;
}
}
// Function to return the
// count of set bits in x
static int countSetBits(long x)
{
// To store the count
// of set bits
int setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
// Function to add num to the answer
// by setting all bit positions as 0
// which are also 0 in K
static void add(long num)
{
long value = 0;
for (int i = 0; i < MAX; i++)
{
// Bit i is 0 in K
if (visited[i])
continue;
else
{
if (num %2== 1)
{
value += (1 << i);
}
num /= 2;
}
}
ans.Add(value);
}
// Function to find and print N distinct
// numbers whose bitwise OR is K
static void solve(long n, long k)
{
// Choosing K itself as one number
ans.Add(k);
// Find the count of set bits in K
int countk = countSetBits(k);
// Impossible to get N
// distinct integers
if (pow2[countk] < n)
{
Console.WriteLine(-1);
return;
}
int count = 0;
for (long i = 0; i < pow2[countk] - 1; i++)
{
// Add i to the answer after
// setting all the bits as 0
// which are 0 in K
add(i);
count++;
// If N distinct numbers are generated
if (count == n)
break;
}
// Print the generated numbers
for (int i = 0; i < n; i++)
{
Console.Write(ans[i]+ " ");
}
}
// Driver code
public static void Main()
{
long n = 3, k = 5;
// Pre-calculate all
// the powers of 2
power_2();
solve(n, k);
}
}
// This code is contributed by ihritik
JavaScript
<script>
// Javascript implementation of the approach
const MAX = 32;
let pow2 = new Array(MAX);
let visited = new Array(MAX);
let ans = [];
// Function to pre-calculate
// all the powers of 2 upto MAX
function power_2()
{
let ans = 1;
for (let i = 0; i < MAX; i++) {
pow2[i] = ans;
ans *= 2;
}
}
// Function to return the
// count of set bits in x
function countSetBits(x)
{
// To store the count
// of set bits
let setBits = 0;
while (x != 0) {
x = x & (x - 1);
setBits++;
}
return setBits;
}
// Function to add num to the answer
// by setting all bit positions as 0
// which are also 0 in K
function add(num)
{
let point = 0;
let value = 0;
for (let i = 0; i < MAX; i++) {
// Bit i is 0 in K
if (visited[i])
continue;
else {
if (num & 1) {
value += (1 << i);
}
num = parseInt(num / 2);
}
}
ans.push(value);
}
// Function to find and print N distinct
// numbers whose bitwise OR is K
function solve(n, k)
{
// Choosing K itself as one number
ans.push(k);
// Find the count of set bits in K
let countk = countSetBits(k);
// Impossible to get N
// distinct integers
if (pow2[countk] < n) {
document.write(-1);
return;
}
let count = 0;
for (let i = 0; i < pow2[countk] - 1; i++) {
// Add i to the answer after
// setting all the bits as 0
// which are 0 in K
add(i);
count++;
// If N distinct numbers are generated
if (count == n)
break;
}
// Print the generated numbers
for (let i = 0; i < n; i++) {
document.write(ans[i] + " ");
}
}
// Driver code
let n = 3, k = 5;
// Pre-calculate all
// the powers of 2
power_2();
solve(n, k);
</script>
Time Complexity: O(MAX)
Auxiliary Space: O(MAX)
Similar Reads
Find N distinct numbers whose Bitwise XOR is equal to K Given two positive integers N and K, the task is to construct N positive integers having Bitwise XOR of all these integers equal to K. Examples: Input: N = 4, K = 6Output: 1 0 2 5Explanation: Bitwise XOR the integers {1, 0, 2, 5} = 1 XOR 0 XOR 2 XOR 5 = 6(= K). Input: N = 1, K = 1Output: 1 Approach:
11 min read
Find numbers in the range L to R whose bitwise OR of digits is exactly K Given integers L and R, the task is to find the number of integers in the range L to R whose bitwise OR of digits is exactly K. Print the answer. Examples: Input: L = 1, R = 15, K =5 Output: 3Explanation: For number 5 its bitwise OR of digits will be 5For number 14 its bitwise OR of digits will be 5
15 min read
Find the count of distinct numbers in a range Given an array of size N containing numbers only from 0 to 63, and you are asked Q queries regarding it.Queries are as follows: 1 X Y i.e Change the element at index X to Y2 L R i.e Print the count of distinct elements present in between L and R inclusive Examples: Input: N = 7 ar = {1, 2, 1, 3, 1,
15 min read
Numbers whose bitwise OR and sum with N are equal Given a non-negative integer N, the task is to find count of non-negative integers less than or equal to N whose bitwise OR and sum with N are equal. Examples : Input : N = 3 Output : 1 0 is the only number in [0, 3] that satisfies given property. (0 + 3) = (0 | 3) Input : 10 Output : 4 (0 + 10) = (
6 min read
Find nth number that contains the digit k or divisible by k. You have given two number n and k. You need to find the n-th number that contains the digit k or divisible by k (2 <= k <=9 ).Examples: Input : n = 15, k = 3 Output : 33 Explanation : ( 3, 6, 9, 12, 13, 15, 18, 21, 23, 24, 27, 30, 31, 33 ). These are those number who contain the digit k = 3 or
7 min read