Construct the Array using given bitwise AND, OR and XOR Last Updated : 17 Apr, 2023 Comments Improve Suggest changes Like Article Like Report Given Bitwise AND, OR, and XOR of N elements of an array denoted by a, b, c. The task is to find the elements of the array. If there exists no such array then print "-1".Examples: Input: N = 3, a = 4, b = 6, c = 6. Output: {4, 4, 6} Explanation: Bitwise AND of array = 4 & 4 & 6 = 4 Bitwise OR of array = 4 | 4 | 6 = 6 Bitwise XOR of array = 4 ^ 4 ^ 6 = 6Input: N = 2, a = 4, b = 6, c = 6. Output: -1 Approach: For Bitwise AND, if ith bit is set in a, then in the array every element must have ith bit set because if even one element's ith bit is 0 then bitwise AND of the array will result in ith bit to be 0.Secondly, if ith bit is not set in a, then OR and XOR values need to be handled simultaneously. If ith bit is set in b, then at least one element must have ith bit set. So, set ith bit in the only first element of the array.Now, if ith bit was set in b then ith bit must be checked in c. If that bit is set in c then there's no problem as the first element's ith bit is already set so 1 ^ 0 = 1. If that bit is not set in c then set the ith bit of the second element. Now, there will not be any effect in b and for c, 1 ^ 1 will be 0.Then, just calculate Bitwise AND, OR, and XOR of the array to check if it's equal or not. If results are not equal then the array is not possible else given array is the answer. Below is the implementation of the approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the array void findArray(int n, int a, int b, int c) { int arr[n + 1] = {}; // Loop through all bits in number for (int bit = 30; bit >= 0; bit--) { // If bit is set in AND // then set it in every element // of the array int set = a & (1 << bit); if (set) { for (int i = 0; i < n; i++) arr[i] |= set; } // If bit is not set in AND else { // But set in b(OR) if (b & (1 << bit)) { // Set bit position // in first element arr[0] |= (1 << bit); // If bit is not set in c // then set it in second // element to keep xor as // zero for bit position if (!(c & (1 << bit))) { arr[1] |= (1 << bit); } } } } int aa = INT_MAX, bb = 0, cc = 0; // Calculate AND, OR // and XOR of array for (int i = 0; i < n; i++) { aa &= arr[i]; bb |= arr[i]; cc ^= arr[i]; } // Check if values are equal or not if (a == aa && b == bb && c == cc) { for (int i = 0; i < n; i++) cout << arr[i] << " "; } // If not, then array // is not possible else cout << "-1"; } // Driver Code int main() { // Given Bitwise AND, OR, and XOR int n = 3, a = 4, b = 6, c = 6; // Function Call findArray(n, a, b, c); return 0; } Java // Java program for the above approach import java.io.*; class GFG{ // Function to find the array static void findArray(int n, int a, int b, int c) { int arr[] = new int[n + 1]; // Loop through all bits in number for(int bit = 30; bit >= 0; bit--) { // If bit is set in AND // then set it in every element // of the array int set = a & (1 << bit); if (set != 0) { for(int i = 0; i < n; i++) arr[i] |= set; } // If bit is not set in AND else { // But set in b(OR) if ((b & (1 << bit)) != 0) { // Set bit position // in first element arr[0] |= (1 << bit); // If bit is not set in c // then set it in second // element to keep xor as // zero for bit position if ((c & (1 << bit)) == 0) { arr[1] |= (1 << bit); } } } } int aa = Integer.MAX_VALUE, bb = 0, cc = 0; // Calculate AND, OR // and XOR of array for(int i = 0; i < n; i++) { aa &= arr[i]; bb |= arr[i]; cc ^= arr[i]; } // Check if values are equal or not if (a == aa && b == bb && c == cc) { for(int i = 0; i < n; i++) System.out.print(arr[i] + " "); } // If not, then array // is not possible else System.out.println("-1"); } // Driver code public static void main(String[] args) { // Given Bitwise AND, OR, and XOR int n = 3, a = 4, b = 6, c = 6; // Function Call findArray(n, a, b, c); } } // This code is contributed by Pratima Pandey Python3 # Python3 program for # the above approach import sys # Function to find the array def findArray(n, a, b, c): arr = [0] * (n + 1) # Loop through all bits in number for bit in range (30, -1, -1): # If bit is set in AND # then set it in every element # of the array set = a & (1 << bit) if (set): for i in range (n): arr[i] |= set # If bit is not set in AND else : # But set in b(OR) if (b & (1 << bit)): # Set bit position # in first element arr[0] |= (1 << bit) # If bit is not set in c # then set it in second # element to keep xor as # zero for bit position if (not (c & (1 << bit))): arr[1] |= (1 << bit) aa = sys.maxsize bb = 0 cc = 0 # Calculate AND, OR # and XOR of array for i in range (n): aa &= arr[i] bb |= arr[i] cc ^= arr[i] # Check if values are equal or not if (a == aa and b == bb and c == cc): for i in range (n): print (arr[i], end = " ") # If not, then array # is not possible else: print ("-1") # Driver Code if __name__ =="__main__": # Given Bitwise AND, OR, and XOR n = 3 a = 4 b = 6 c = 6 # Function Call findArray(n, a, b, c) # This code is contributed by Chitranayal C# // C# program for the above approach using System; class GFG{ // Function to find the array static void findArray(int n, int a, int b, int c) { int []arr = new int[n + 1]; // Loop through all bits in number for(int bit = 30; bit >= 0; bit--) { // If bit is set in AND // then set it in every element // of the array int set = a & (1 << bit); if (set != 0) { for(int i = 0; i < n; i++) arr[i] |= set; } // If bit is not set in AND else { // But set in b(OR) if ((b & (1 << bit)) != 0) { // Set bit position // in first element arr[0] |= (1 << bit); // If bit is not set in c // then set it in second // element to keep xor as // zero for bit position if ((c & (1 << bit)) == 0) { arr[1] |= (1 << bit); } } } } int aa = int.MaxValue, bb = 0, cc = 0; // Calculate AND, OR // and XOR of array for(int i = 0; i < n; i++) { aa &= arr[i]; bb |= arr[i]; cc ^= arr[i]; } // Check if values are equal or not if (a == aa && b == bb && c == cc) { for(int i = 0; i < n; i++) Console.Write(arr[i] + " "); } // If not, then array // is not possible else Console.WriteLine("-1"); } // Driver code public static void Main(String[] args) { // Given Bitwise AND, OR, and XOR int n = 3, a = 4, b = 6, c = 6; // Function Call findArray(n, a, b, c); } } // This code is contributed by gauravrajput1 JavaScript <script> // Javascript program for the above approach // Function to find the array function findArray(n, a, b, c) { let arr = new Array(n + 1); // Loop through all bits in number for (let bit = 30; bit >= 0; bit--) { // If bit is set in AND // then set it in every element // of the array let set = a & (1 << bit); if (set) { for (let i = 0; i < n; i++) arr[i] |= set; } // If bit is not set in AND else { // But set in b(OR) if (b & (1 << bit)) { // Set bit position // in first element arr[0] |= (1 << bit); // If bit is not set in c // then set it in second // element to keep xor as // zero for bit position if (!(c & (1 << bit))) { arr[1] |= (1 << bit); } } } } let aa = Number.MAX_SAFE_INTEGER, bb = 0, cc = 0; // Calculate AND, OR // and XOR of array for (let i = 0; i < n; i++) { aa &= arr[i]; bb |= arr[i]; cc ^= arr[i]; } // Check if values are equal or not if (a == aa && b == bb && c == cc) { for (let i = 0; i < n; i++) document.write(arr[i] + " "); } // If not, then array // is not possible else document.write("-1"); } // Driver Code // Given Bitwise AND, OR, and XOR let n = 3, a = 4, b = 6, c = 6; // Function Call findArray(n, a, b, c); // This code is contributed by gfgking </script> Output: 6 4 4 Time Complexity: O(31*N) Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Construct the Array using given bitwise AND, OR and XOR sarthak_eddy Follow Improve Article Tags : Bit Magic Algorithms Competitive Programming DSA Arrays Bitwise-XOR Bitwise-OR Bitwise-AND +4 More Practice Tags : AlgorithmsArraysBit Magic Similar Reads Construct a sorted Array such that setbit in bitwise XOR of any pair is even Given an integer N(1 ⤠N ⤠500), Construct an integer arr[] of length N, such that it should follow all the given conditions below: Each element of arr[] should be distinct.Binary representation of (arr[i]^arr[j]) should contain even number of set bits for all ( 1â¤iâ¤N ) and ( iâ j ).All the elements 7 min read Construct the smallest possible Array with given Sum and XOR Given two positive integers S and X which represents the sum and Bitwise XOR of all the elements of an array arr[]. The task is to find the elements of the array arr[]. If no such array can be generated, print -1.Examples: Input: Sum = 4, Xor = 2 Output: {3, 1} Explanation: Sum of 1 and 3 is 4. Bitw 7 min read Bitwise XOR of Bitwise AND of all pairs from two given arrays Given two arrays arr1[] and arr2[] consisting of N and M integers respectively, the task is to print the Bitwise XOR of Bitwise AND of all pairs possible by selecting an element from arr1[] and arr2[]. Examples: Input: arr1[] = {1, 2, 3}, arr2[] = {6, 5}Output: 0Explanation: Bitwise AND of the pair 10 min read Find XOR sum of Bitwise AND of all pairs from given two Arrays Given two arrays A and B of sizes N and M respectively, the task is to calculate the XOR sum of bitwise ANDs of all pairs of A and B Examples: Input: A={3, 5}, B={2, 3}, N=2, M=2Output:0Explanation:The answer is (3&2)^(3&3)^(5&2)^(5&3)=1^3^0^2=0. Input: A={1, 2, 3}, B={5, 6}, N=3, M= 9 min read Bitwise AND and XOR pair counting Given an integer array arr[] of size N, the task is to count the number of pairs whose BITWISE AND and BITWISE XOR are equal. Examples: Input: N = 3, arr[] = [0, 0, 1]Output: 1Explanation: All possible pairs from the array are pair = [(0, 0), (0, 1), (1, 0)]. we can see that pair = (0, 0), 0&0 = 7 min read Form an Array so that their Bitwise XOR sum satisfies given conditions Given an array arr[] of size N ( N is even ), the task is to construct an array B[] such that the sum of B[i] XOR PrefixXor[i], where 1 ? i ? N is even and is divisible by the first N/2 elements of arr[]. PrefixXor[] array is an array where each element will indicate the XOR of all elements from 1st 7 min read Minimizing Bitwise-OR with XOR Operation on an Array Given an array arr[] of non-negative integers where arr[i] >= 0, the task is to select a non-negative integer 'k' and perform the bitwise-XOR operation on every element of the array with 'k' (i.e., XOR0 = arr[0] ^ k, XOR1 = arr[1] ^ k, and so on). The objective is to minimize the bitwise-OR of th 9 min read Find bitwise XOR of all triplets formed from given three Arrays Given three arrays arr1[], arr2[], and arr3[] consisting of non-negative integers. The task is to find the bitwise XOR of the XOR of all possible triplets that are formed by taking one element from each array. Examples: Input: arr1[] = {2, 3, 1}, arr2[] = {2, 4}, arr3[] = {3, 5}Output: 0Explanation: 11 min read Bitwise AND of all the elements of array Given an array, arr[] of N integers, the task is to find out the bitwise AND(&) of all the elements of the array. Examples: Input: arr[] = {1, 3, 5, 9, 11} Output: 1 Input: arr[] = {3, 7, 11, 19, 11} Output: 3 Approach: The idea is to traverse all the array elements and compute the bitwise AND f 4 min read Check if any Array pair has bitwise XOR greater than bitwise AND Given an array arr[] of size N, the task is to find if there exists a pair in the array, such that their bitwise XOR is greater than their bitwise AND i.e. arr[i] â arr[j] > arr[i] & arr[j], (0 ⤠i < j ⤠N-1) where â represents the Bitwise XOR operator and & represents bitwise AND oper 9 min read Like