Form a bitwise equation from L+1 integers which gives result N
Given two integers N and L, Find out a string of length L which consists of bitwise operators &(AND), |(OR), and ^(XOR), and if these operators are placed in order between L+1 integers, (where each integer value lies between 0 to N ) it forms a bitwise equation which gives result N, also it must follow that any two consecutive characters of a string cannot be the same(&&|&^ is not acceptable but &|&|^ is acceptable).
Note: In the bitwise equation use operators work from left to right and If multiple outputs are possible, Print any.
- If string is &|^&^|, then equation can be 1&1|5^4&8^9|7.
- And operators work like this 1&1|5^4&8^9|7 = 1|5^4&8^9|7 = 5^4&8^9|7 = 1&8^9|7 = 0^9|7 = 9|7 = 15.
Examples:
Input: N = 5, L = 6
Output: &|&|&^
Explanation: 1&1|1&1|1&2^5 = 5
Input: N = 3, L = 2
Output: &^
Approach: To solve the problem follow the below idea:
We will use two bitwise property to solve this question:
- A XOR 0 = A
- A AND (A – 1) = 0, if A is power of 2.
We will apply first property in the end of the string, means we operate XOR between N and 0 thus we will get the number N. And other elements of string will be AND and OR operator to get zero. In the bitwise equation, last element will be the number N and previous number will be 0 and other previous numbers are 1, then this 1 & 0 will be 0 and lastly 0^N=N.
Steps were to follow the approach:
- Check if the given length of the required string is odd or even.
- Initialize a resultant string say str.
- If the length is even then we all know the last element must be ^ in this approach and the previous element of ‘^’ must be &.
- Push back ‘&’ to the string.
- Run a loop from i = 0 till i < (K-2)/2 and push back consecutive ‘|’ and ‘&’ to the resultant string.
- Push ‘^’ as the last element in the resultant string.
- If the length is odd,
- Run a loop from i = 0 till i < (K/2) and push back consecutive ‘|’ and ‘&’ to the resultant string.
- Push back ‘^’ as the last element in the resultant string.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
string bitwise( int n, int k)
{
string str;
if (k % 2 == 0) {
str.push_back( '&' );
for ( int i = 0; i < (k - 2) / 2; i++) {
str.push_back( '|' );
str.push_back( '&' );
}
str.push_back( '^' );
}
else {
for ( int i = 0; i < k / 2; i++) {
str.push_back( '|' );
str.push_back( '&' );
}
str.push_back( '^' );
}
return str;
}
int main()
{
int n = 5, k = 6;
string ans;
ans = bitwise(n, k);
for ( int i = 0; i < ans.length(); i++) {
cout << ans[i];
}
cout << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static String bitwise( int n, int k)
{
StringBuilder str = new StringBuilder();
if (k % 2 == 0 ) {
str.append( '&' );
for ( int i = 0 ; i < (k - 2 ) / 2 ; i++) {
str.append( '|' );
str.append( '&' );
}
str.append( '^' );
}
else {
for ( int i = 0 ; i < k / 2 ; i++) {
str.append( '|' );
str.append( '&' );
}
str.append( '^' );
}
return str.toString();
}
public static void main(String[] args)
{
int n = 5 , k = 6 ;
String ans;
ans = bitwise(n, k);
for ( int i = 0 ; i < ans.length(); i++) {
System.out.print(ans.charAt(i));
}
System.out.println();
}
}
|
Python3
def bitwise(n, k):
str = ""
if k % 2 = = 0 :
str + = '&'
for i in range ((k - 2 ) / / 2 ):
str + = '|'
str + = '&'
str + = '^'
else :
for i in range (k / / 2 ):
str + = '|'
str + = '&'
str + = '^'
return str
if __name__ = = '__main__' :
n = 5
k = 6
ans = bitwise(n, k)
print (ans)
|
C#
using System;
public class Program
{
public static string bitwise( int n, int k)
{
string str = "" ;
if (k % 2 == 0)
{
str += '&' ;
for ( int i = 0; i < (k - 2) / 2; i++)
{
str += '|' ;
str += '&' ;
}
str += '^' ;
}
else
{
for ( int i = 0; i < k / 2; i++)
{
str += '|' ;
str += '&' ;
}
str += '^' ;
}
return str;
}
public static void Main()
{
int n = 5, k = 6;
string ans = bitwise(n, k);
foreach ( char c in ans)
{
Console.Write(c);
}
Console.WriteLine();
}
}
|
Javascript
function bitwise(n, k) {
let str = "" ;
if (k % 2 === 0) {
str += "&" ;
for (let i = 0; i < (k - 2) / 2; i++) {
str += "|" ;
str += "&" ;
}
str += "^" ;
}
else {
for (let i = 0; i < k / 2; i++) {
str += "|" ;
str += "&" ;
}
str += "^" ;
}
return str;
}
let n = 5, k = 6;
let ans = "" ;
ans = bitwise(n, k);
for (let i = 0; i < ans.length; i++) {
console.log(ans[i]);
}
|
Time Complexity: O(K), K is the given length of the string.
Auxiliary Space: O(K)
Last Updated :
13 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...