Find array such that no subarray has xor zero or Y
Last Updated :
05 Sep, 2022
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 exist multiple answers then print any one of them. If no such array exists then print -1
Examples:
Input: X = 3, Y = 5
Output: 1 3 1
(1 ^ 3) = 2
(3 ^ 1) = 2
(1 ^ 3 ^ 1) = 3
Input: X = 1, Y = 1
Output: -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 original array using: arr[i] = pre[i] ^ pre[i - 1].
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum length array
void maxLengthArr(int x, int y)
{
// To store if an element is
// already taken or not
bool ex[(1 << x)];
// To store visited numbers
ex[0] = 1;
vector<int> pre({ 0 });
// For all possible values of pre[]
for (int i = 1; i < (1 << x); i++) {
// If it is already taken
if (ex[i ^ y])
continue;
pre.push_back(i);
ex[i] = 1;
}
// Not possible
if (pre.size() == 1) {
cout << "-1";
return;
}
// Print the array constructing it
// from the prefix-xor array
for (int i = 1; i < pre.size(); i++)
cout << (pre[i] ^ pre[i - 1]) << " ";
}
// Driver code
int main()
{
int X = 3, Y = 5;
maxLengthArr(X, Y);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to find the maximum length array
static void maxLengthArr(int x, int y)
{
// To store if an element is
// already taken or not
boolean[] ex = new boolean[(1 << x)];
// To store visited numbers
ex[0] = true;
Vector<Integer> pre = new Vector<Integer>();
pre.add(0);
// For all possible values of pre[]
for (int i = 1; i < (1 << x); i++)
{
// If it is already taken
if (ex[i ^ y])
continue;
pre.add(i);
ex[i] = true;
}
// Not possible
if (pre.size() == 1)
{
System.out.print("-1");
return;
}
// Print the array constructing it
// from the prefix-xor array
for (int i = 1; i < pre.size(); i++)
System.out.print((pre.get(i) ^
pre.get(i - 1)) + " ");
}
// Driver code
public static void main(String[] args)
{
int X = 3, Y = 5;
maxLengthArr(X, Y);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function to find the maximum length array
def maxLengthArr(x, y) :
# To store if an element is
# already taken or not
ex = [0] * (1 << x);
# To store visited numbers
ex[0] = 1;
pre = [0];
# For all possible values of pre[]
for i in range(1, (1 << x)) :
# If it is already taken
if (ex[i ^ y]) :
continue;
pre.append(i);
ex[i] = 1;
# Not possible
if (len(pre) == 1) :
print("-1", end = "");
return;
# Print the array constructing it
# from the prefix-xor array
for i in range(1, len(pre)) :
print(pre[i] ^ pre[i - 1], end = " ");
# Driver code
if __name__ == "__main__" :
X = 3; Y = 5;
maxLengthArr(X, Y);
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the maximum length array
static void maxLengthArr(int x, int y)
{
// To store if an element is
// already taken or not
bool[] ex = new bool[(1 << x)];
// To store visited numbers
ex[0] = true;
var pre = new List<int>();
pre.Add(0);
// For all possible values of pre[]
for (int i = 1; i < (1 << x); i++)
{
// If it is already taken
if (ex[i ^ y])
continue;
pre.Add(i);
ex[i] = true;
}
// Not possible
if (pre.Count == 1)
{
Console.Write("-1");
return;
}
// Print the array constructing it
// from the prefix-xor array
for (int i = 1; i < pre.Count; i++)
Console.Write((pre[i] ^ pre[i - 1]) + " ");
}
// Driver code
public static void Main(String[] args)
{
int X = 3, Y = 5;
maxLengthArr(X, Y);
}
}
// This code is contributed by
// sanjeev2552
JavaScript
<script>
// Javascript implementation of the approach
// Function to find the maximum length array
function maxLengthArr(x, y)
{
// To store if an element is
// already taken or not
let ex = new Array((1 << x)).fill(0);
// To store visited numbers
ex[0] = 1;
let pre = [];
pre.push(0);
// For all possible values of pre[]
for (let i = 1; i < (1 << x); i++) {
// If it is already taken
if (ex[i ^ y])
continue;
pre.push(i);
ex[i] = 1;
}
// Not possible
if (pre.length == 1) {
document.write("-1");
return;
}
// Print the array constructing it
// from the prefix-xor array
for (let i = 1; i < pre.length; i++)
document.write((pre[i] ^ pre[i - 1]) + " ");
}
// Driver code
let X = 3, Y = 5;
maxLengthArr(X, Y);
</script>
Time complexity: O(2x) where x is the given input
Auxiliary space: O(2x) where x is the given input
Related Topic: Subarrays, Subsequences, and Subsets in Array
Similar Reads
Bit Manipulation for Competitive Programming Bit manipulation is a technique in competitive programming that involves the manipulation of individual bits in binary representations of numbers. It is a valuable technique in competitive programming because it allows you to solve problems efficiently, often reducing time complexity and memory usag
15+ min read
Count set bits in an integer Write an efficient program to count the number of 1s in the binary representation of an integer.Examples : Input : n = 6Output : 2Binary representation of 6 is 110 and has 2 set bitsInput : n = 13Output : 3Binary representation of 13 is 1101 and has 3 set bits[Naive Approach] - One by One CountingTh
15+ min read
Count total set bits in first N Natural Numbers (all numbers from 1 to N) Given a positive integer n, the task is to count the total number of set bits in binary representation of all natural numbers from 1 to n. Examples: Input: n= 3Output: 4Explanation: Numbers from 1 to 3: {1, 2, 3}Binary Representation of 1: 01 -> Set bits = 1Binary Representation of 2: 10 -> Se
9 min read
Check whether the number has only first and last bits set Given a positive integer n. The problem is to check whether only the first and last bits are set in the binary representation of n.Examples: Input : 9 Output : Yes (9)10 = (1001)2, only the first and last bits are set. Input : 15 Output : No (15)10 = (1111)2, except first and last there are other bi
4 min read
Shortest path length between two given nodes such that adjacent nodes are at bit difference 2 Given an unweighted and undirected graph consisting of N nodes and two integers a and b. The edge between any two nodes exists only if the bit difference between them is 2, the task is to find the length of the shortest path between the nodes a and b. If a path does not exist between the nodes a and
7 min read
Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values Given two integers X and Y, representing Bitwise XOR and Bitwise AND of two positive integers, the task is to calculate the Bitwise OR value of those two positive integers.Examples:Input: X = 5, Y = 2 Output: 7 Explanation: If A and B are two positive integers such that A ^ B = 5, A & B = 2, the
7 min read
Unset least significant K bits of a given number Given an integer N, the task is to print the number obtained by unsetting the least significant K bits from N. Examples: Input: N = 200, K=5Output: 192Explanation: (200)10 = (11001000)2 Unsetting least significant K(= 5) bits from the above binary representation, the new number obtained is (11000000
4 min read
Find all powers of 2 less than or equal to a given number Given a positive number N, the task is to find out all the perfect powers of two which are less than or equal to the given number N. Examples: Input: N = 63 Output: 32 16 8 4 2 1 Explanation: There are total of 6 powers of 2, which are less than or equal to the given number N. Input: N = 193 Output:
6 min read
Powers of 2 to required sum Given an integer N, task is to find the numbers which when raised to the power of 2 and added finally, gives the integer N. Example : Input : 71307 Output : 0, 1, 3, 7, 9, 10, 12, 16 Explanation : 71307 = 2^0 + 2^1 + 2^3 + 2^7 + 2^9 + 2^10 + 2^12 + 2^16 Input : 1213 Output : 0, 2, 3, 4, 5, 7, 10 Exp
10 min read
Print bitwise AND set of a number N Given a number N, print all the numbers which are a bitwise AND set of the binary representation of N. Bitwise AND set of a number N is all possible numbers x smaller than or equal N such that N & i is equal to x for some number i. Examples : Input : N = 5Output : 0, 1, 4, 5 Explanation: 0 &
8 min read