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 = 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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MAX 32
ll pow2[MAX];
bool visited[MAX];
vector< int > ans;
void power_2()
{
ll ans = 1;
for ( int i = 0; i < MAX; i++) {
pow2[i] = ans;
ans *= 2;
}
}
int countAndCheckSetBits(ll x)
{
int setBits = 0;
for ( int i = 0; i < MAX; i++) {
int a = (x & (1 << i));
if (a != 0) {
visited[i] = true ;
setBits++;
}
else {
visited[i] = false ;
}
}
return setBits;
}
void add(ll num)
{
int point = 0;
ll value = 0;
for (ll i = 0; i < MAX; i++) {
if (visited[i]) {
if (num & 1) {
value += (1 << i);
}
num /= 2;
}
else {
continue ;
}
}
ans.push_back(value);
}
void solve(ll n, ll k)
{
ans.push_back(k);
int countk = countAndCheckSetBits(k);
if (pow2[countk] < n) {
cout << -1;
return ;
}
int count = 0;
for (ll i = 0; i < pow2[countk] - 1; i++) {
add(i);
count++;
if (count == n)
break ;
}
for ( int i = 0; i < n; i++) {
cout << ans[i] << " " ;
}
}
int main()
{
ll n = 4, k = 20;
power_2();
solve(n, k);
return 0;
}
|
Java
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<>();
static void power_2()
{
long ans = 1 ;
for ( int i = 0 ; i < MAX; i++)
{
pow2[i] = ans;
ans *= 2 ;
}
}
static int countSetBits( long x)
{
int setBits = 0 ;
while (x != 0 )
{
x = x & (x - 1 );
setBits++;
}
return setBits;
}
static void add( long num)
{
int point = 0 ;
long value = 0 ;
for ( int i = 0 ; i < MAX; i++)
{
if (visited[i])
continue ;
else
{
if (num % 2 == 1 )
{
value += ( 1 << i);
}
num /= 2 ;
}
}
ans.add(value);
}
static void solve( long n, long k)
{
ans.add(k);
int countk = countSetBits(k);
if (pow2[countk] < n)
{
System.out.print(- 1 );
return ;
}
int count = 0 ;
for ( long i = 0 ; i < pow2[countk] - 1 ; i++)
{
add(i);
count++;
if (count == n)
break ;
}
for ( int i = 0 ; i < n; i++)
{
System.out.print(ans.get(i)+ " " );
}
}
public static void main(String[] args)
{
long n = 3 , k = 5 ;
power_2();
solve(n, k);
}
}
|
Python3
MAX = 32
pow2 = [ 0 for i in range ( MAX )]
visited = [ False for i in range ( MAX )]
ans = []
def power_2():
an = 1
for i in range ( MAX ):
pow2[i] = an
an * = 2
def countSetBits(x):
setBits = 0
while (x ! = 0 ):
x = x & (x - 1 )
setBits + = 1
return setBits
def add(num):
point = 0
value = 0
for i in range ( MAX ):
if (visited[i]):
continue
else :
if (num & 1 ):
value + = ( 1 << i)
num = num / / 2
ans.append(value)
def solve(n, k):
ans.append(k)
countk = countSetBits(k)
if (pow2[countk] < n):
print ( - 1 )
return
count = 0
for i in range (pow2[countk] - 1 ):
add(i)
count + = 1
if (count = = n):
break
for i in range (n):
print (ans[i],end = " " )
if __name__ = = '__main__' :
n = 3
k = 5
power_2()
solve(n, k)
|
C#
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 >();
static void power_2()
{
long ans = 1;
for ( int i = 0; i < MAX; i++)
{
pow2[i] = ans;
ans *= 2;
}
}
static int countSetBits( long x)
{
int setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
static void add( long num)
{
long value = 0;
for ( int i = 0; i < MAX; i++)
{
if (visited[i])
continue ;
else
{
if (num %2== 1)
{
value += (1 << i);
}
num /= 2;
}
}
ans.Add(value);
}
static void solve( long n, long k)
{
ans.Add(k);
int countk = countSetBits(k);
if (pow2[countk] < n)
{
Console.WriteLine(-1);
return ;
}
int count = 0;
for ( long i = 0; i < pow2[countk] - 1; i++)
{
add(i);
count++;
if (count == n)
break ;
}
for ( int i = 0; i < n; i++)
{
Console.Write(ans[i]+ " " );
}
}
public static void Main()
{
long n = 3, k = 5;
power_2();
solve(n, k);
}
}
|
Javascript
<script>
const MAX = 32;
let pow2 = new Array(MAX);
let visited = new Array(MAX);
let ans = [];
function power_2()
{
let ans = 1;
for (let i = 0; i < MAX; i++) {
pow2[i] = ans;
ans *= 2;
}
}
function countSetBits(x)
{
let setBits = 0;
while (x != 0) {
x = x & (x - 1);
setBits++;
}
return setBits;
}
function add(num)
{
let point = 0;
let value = 0;
for (let i = 0; i < MAX; i++) {
if (visited[i])
continue ;
else {
if (num & 1) {
value += (1 << i);
}
num = parseInt(num / 2);
}
}
ans.push(value);
}
function solve(n, k)
{
ans.push(k);
let countk = countSetBits(k);
if (pow2[countk] < n) {
document.write(-1);
return ;
}
let count = 0;
for (let i = 0; i < pow2[countk] - 1; i++) {
add(i);
count++;
if (count == n)
break ;
}
for (let i = 0; i < n; i++) {
document.write(ans[i] + " " );
}
}
let n = 3, k = 5;
power_2();
solve(n, k);
</script>
|
Time Complexity: O(MAX)
Auxiliary Space: O(MAX)
Last Updated :
14 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...