Given two integers X (1 ≤ X ≤ 15) and Y. The task is to find an array of the maximum possible length N such that all the elements in the array lie in between 1 and 2X and there exists no subarray such that xor value of the subarray is either 0 or Y. If there exists multiple answers then print any one of them. If no such array exists then print -1
Input: X = 3, Y = 5
Output: 1 3 1
(1 ^ 3) = 2
(3 ^ 1) = 2
(1 ^ 3 ^ 1) = 3
Input: X = 1, Y = 1
Approach: The main idea is to build the prefix-xor of the required array and then build the array from it. Let the prefix-xor array be pre.
Now, XOR of any two pairs in the prefix array say (pre[l] ^ pre[r]) will represent the XOR of some sub-array of the original array i.e. arr[l + 1] ^ arr[l + 2] ^ … ^ arr[r].
Thus, the problem now reduces to construct an array from the elements of pre array such that no pair of elements have bitwise-xor equal to 0 or Y and its length should be maximal.
Notice that no pair of numbers has a bitwise-xor sum equal to 0 simply means can’t use the same number twice.
If Y ≥ 2X then no pair of numbers less than 2X will have a bitwise-xor equal to Y, so you can just use all the numbers from 1 to 2X – 1 in any order. Otherwise, you can think of the numbers forming pairs, where each pair consists of 2 numbers with a bitwise-xor sum equal to Y. From any pair, if you add one number to the array, you can’t add the other. However, the pairs are independent of each other: your choice in one pair doesn’t affect any other pair. Thus, you can just choose either number in any pair and add them in any order you want. After you construct array pre, you can construct the orginal array using: arr[i] = pre[i] ^ pre[i – 1].
Below is the implementation of the above approach:
1 3 1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a number from every row can be selected such that xor of the numbers is greater than zero
- Find all possible triangles with XOR of sides zero
- Minimum operations to make XOR of array zero
- Quadruplet pair with XOR zero in the given Array
- Number of ways to erase exactly one element in the Binary Array to make XOR zero
- Choose X such that (A xor X) + (B xor X) is minimized
- Maximum length of subarray such that sum of the subarray is even
- Maximum length of subarray such that all elements are equal in the subarray
- Check if the array has an element which is equal to XOR of remaining elements
- Ways to remove one element from a binary string so that XOR becomes zero
- Number of unique triplets whose XOR is zero
- Maximize the number of subarrays with XOR as zero
- Check if the Xor of the frequency of all digits of a number N is zero or not
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Find the maximum subarray XOR in a given array
- Longest Subarray with Sum greater than Equal to Zero
- Given a set, find XOR of the XOR's of all subsets.
- Find XOR of two number without using XOR operator
- Find the subarray of size K with minimum XOR
- Number of triplets in array having subarray xor equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.