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 DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an 8 min read Like