Open In App

Form a bitwise equation from L+1 integers which gives result N

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ Code for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to get the bitwise string
string bitwise(int n, int k)
{
 
    // Take a string str in which we push
    // back the bitwise operators
    string str;
 
    // If output string's length is even
    // then at last we push back "^",
    // first element will be "&" and
    // previous elements will be
    // consecutive "|" and "&"
    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('^');
    }
 
    // If output string's length is odd
    // then at last we push back "^"
    // and previous elements will be
    // consecutive "|" and "&"
    else {
 
        for (int i = 0; i < k / 2; i++) {
            str.push_back('|');
            str.push_back('&');
        }
        str.push_back('^');
    }
    return str;
}
 
// Driver Code
int main()
{
    int n = 5, k = 6;
    string ans;
    ans = bitwise(n, k);
 
    // Print the string
    for (int i = 0; i < ans.length(); i++) {
        cout << ans[i];
    }
    cout << endl;
 
    return 0;
}


Java




// Java Code for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to get the bitwise string
    static String bitwise(int n, int k)
    {
        // Take a string str in which we push back the
        // bitwise operators
        StringBuilder str = new StringBuilder();
 
        // If output string's length is even then at last we
        // push back "^", first element will be "&" and
        // previous elements will be consecutive "|" and "&"
        if (k % 2 == 0) {
            str.append('&');
            for (int i = 0; i < (k - 2) / 2; i++) {
                str.append('|');
                str.append('&');
            }
            str.append('^');
        }
 
        // If output string's length is odd then at last we
        // push back "^" and previous elements will be
        // consequtive "|" and "&"
        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);
 
        // Print the string
        for (int i = 0; i < ans.length(); i++) {
            System.out.print(ans.charAt(i));
        }
        System.out.println();
    }
}
 
// This code is contributed by lokesh.


Python3




# Python Code for the above approach
 
# Function to get the bitwise string
 
 
def bitwise(n, k):
 
    # Take a string str in which we push
    # back the bitwise operators
    str = ""
 
    # If output string's length is even
    # then at last we push back "^",
    # first element will be "&" and
    # previous elements will be
    # consecutive "|" and "&"
    if k % 2 == 0:
        str += '&'
        for i in range((k - 2) // 2):
            str += '|'
            str += '&'
        str += '^'
 
    # If output string's length is odd
    # then at last we push back "^"
    # and previous elements will be
    # consecutive "|" and "&"
    else:
        for i in range(k // 2):
            str += '|'
            str += '&'
        str += '^'
    return str
 
 
# Driver Code
if __name__ == '__main__':
    n = 5
    k = 6
    ans = bitwise(n, k)
 
    # Print the string
    print(ans)


C#




using System;
 
public class Program
{
    // Function to get the bitwise string
    public static string bitwise(int n, int k)
    {
        // Take a string str in which we push
        // back the bitwise operators
        string str = "";
 
        // If output string's length is even
        // then at last we push back "^",
        // first element will be "&" and
        // previous elements will be
        // consecutive "|" and "&"
        if (k % 2 == 0)
        {
            str += '&';
            for (int i = 0; i < (k - 2) / 2; i++)
            {
                str += '|';
                str += '&';
            }
            str += '^';
        }
 
        // If output string's length is odd
        // then at last we push back "^"
        // and previous elements will be
        // consecutive "|" and "&"
        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);
 
        // Print the string
        foreach (char c in ans)
        {
            Console.Write(c);
        }
        Console.WriteLine();
    }
}


Javascript




// JavaScript code for the above approach
function bitwise(n, k) {
 
    // Take a string str in which we push
    // back the bitwise operators
    let str = "";
     
    // If output string's length is even
    // then at last we push back "^",
    // first element will be "&" and
    // previous elements will be
    // consequtive "|" and "&"
    if (k % 2 === 0) {
        str += "&";
        for (let i = 0; i < (k - 2) / 2; i++) {
            str += "|";
            str += "&";
        }
        str += "^";
    }
     
    // If output string's length is odd
    // then at last we push back "^"
    // and previous elements will be
    // consequtive "|" and "&"
    else {
     
        for (let i = 0; i < k / 2; i++) {
            str += "|";
            str += "&";
        }
        str += "^";
    }
    return str;
}
 
// Driver Code
let n = 5, k = 6;
let ans = "";
ans = bitwise(n, k);
 
// Print the string
for (let i = 0; i < ans.length; i++) {
    console.log(ans[i]);
}


Output

&|&|&^

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
Previous
Next
Share your thoughts in the comments
Similar Reads