Form an Array so that their Bitwise XOR sum satisfies given conditions
Last Updated :
28 Feb, 2023
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 element to the current index element.
Examples:
Input: N = 4, arr[] = {2, 6, 1, 5}
Output: B[] = {0, 6, 3, 6}
Explanation: Since, prefix array is prefixXor[] = {2, 4, 5, 0}, (2 ^ 0) + (4 ^ 6) + (5 ^ 3) + (0^6) = 2 + 2 + 6 + 6 = 16, which is even and divisible by 8.
Input: N = 6, arr[] = {2, 5, 3, 4, 6, 1}
Output: B[] = {0, 5, 1, 5, 5, 4}
Explanation: Since prefix, the array is prefixXor[]={2, 7, 4, 0, 6, 7}, So, (2 ^ 0) + (7 ^ 5) + (4 ^ 1) + (0 ^ 5) + (6 ^ 5) + (7 ^ 4) = 2 + 2 + 5 + 5 + 3 + 3 = 20, which is even and divisible by 10.
Approach: Implement the idea below to solve the problem:
Let's first analyze the question. The given sum should be even and should be divisible by the first N/2 elements. So more formally it means :
if array arr[] = {a, b, c, d}, then its prefix xor is Prefix_xor[]=[a, a^b, a^b^c, a^b^c^d}
Let assume array B[] = {x, y, z, w} then S= (x^a) + (y^(a^b)) + (z^(a^b^c)) + ( w^(a^b^c^d)). So according to the question:
So on combining we get S % (2*(a + b)) == 0
Steps were taken to solve the problem:
- Initialize the pointer = 0 variable to iterate over array arr[].
- Calculate prefixXor[] array.
- Initialize array B[] of size N.
- While iterating over the array, if the difference == 2, increment the pointer, otherwise:
- Store arr[pointer] ^ prefixXor[i] in B[i].
- Increment difference by 1.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to generates valid array
// according to the given conditions
void valid_array_formation(int N, int arr[])
{
int prefix_xor[N] = { arr[0] };
// Calculating the prefix xor array
for (int i = 1; i < N; i++) {
prefix_xor[i] = prefix_xor[i - 1] ^ arr[i];
}
int B[N] = { 0 };
// Pointer to extract which element
// is to be extracted from array a
int pointer = 0;
int difference = 0;
for (int i = 0; i < N; i++) {
// If difference becomes 2 then we
// increment the pointer
if (difference == 2) {
pointer++;
difference = 0;
}
// B[i] i calculated accordingly
B[i] = prefix_xor[i] ^ arr[pointer];
difference++;
}
for (int i = 0; i < N; i++) {
// Printing the required array b
cout << B[i] << " ";
}
cout << endl;
}
// Driver code
int main()
{
// Test case 1
int N = 4;
int arr1[] = { 2, 6, 1, 5 };
// Function call
valid_array_formation(N, arr1);
// Test case 2
N = 6;
int arr2[] = { 2, 5, 3, 4, 6, 1 };
// Function call
valid_array_formation(N, arr2);
return 0;
}
Java
// Java code to implement the approach
import java.util.*;
class GFG {
// Function to generates valid array
// according to the given conditions
public static void valid_array_formation(int N,
int arr[])
{
int prefix_xor[] = new int[N];
prefix_xor[0] = arr[0];
// Calculating the prefix xor array
for (int i = 1; i < N; i++) {
prefix_xor[i] = prefix_xor[i - 1] ^ arr[i];
}
int B[] = new int[N];
// Pointer to extract which element
// is to be extracted from array a
int pointer = 0;
int difference = 0;
for (int i = 0; i < N; i++) {
// If difference becomes 2 then we
// increment the pointer
if (difference == 2) {
pointer++;
difference = 0;
}
// B[i] i calculated accordingly
B[i] = prefix_xor[i] ^ arr[pointer];
difference++;
}
for (int i = 0; i < N; i++) {
// Printing the required array b
System.out.print(B[i] + " ");
}
System.out.println();
}
// Driver code
public static void main(String args[])
{
// Test case 1
int N = 4;
int arr1[] = { 2, 6, 1, 5 };
// Function call
valid_array_formation(N, arr1);
// Test case 2
N = 6;
int arr2[] = { 2, 5, 3, 4, 6, 1 };
// Function call
valid_array_formation(N, arr2);
}
}
// This Code is Contributed by Prasad Kandekar(prasad264)
JavaScript
// Function to generates valid array
// according to the given conditions
function validArrayFormation(N, arr) {
let prefixXor = [arr[0]];
// Calculating the prefix xor array
for (let i = 1; i < N; i++) {
prefixXor.push(prefixXor[i - 1] ^ arr[i]);
}
let B = Array(N).fill(0);
// Pointer to extract which element
// is to be extracted from array a
let pointer = 0;
let difference = 0;
for (let i = 0; i < N; i++) {
// If difference becomes 2 then we
// increment the pointer
if (difference == 2) {
pointer++;
difference = 0;
}
// B[i] i calculated accordingly
B[i] = prefixXor[i] ^ arr[pointer];
difference++;
}
// Printing the required array b
console.log(B.join(" "));
}
// Test case 1
let N = 4;
let arr1 = [2, 6, 1, 5];
// Function call
validArrayFormation(N, arr1);
// Test case 2
N = 6;
let arr2 = [2, 5, 3, 4, 6, 1];
// Function call
validArrayFormation(N, arr2);
Python3
# Function to generates valid array
# according to the given conditions
def valid_array_formation(N, arr):
prefix_xor = [arr[0]]
# Calculating the prefix xor array
for i in range(1, N):
prefix_xor.append(prefix_xor[i-1] ^ arr[i])
B = [0] * N
# Pointer to extract which element
# is to be extracted from array a
pointer = 0
difference = 0
for i in range(N):
# If difference becomes 2 then we
# increment the pointer
if difference == 2:
pointer += 1
difference = 0
# B[i] i calculated accordingly
B[i] = prefix_xor[i] ^ arr[pointer]
difference += 1
# Printing the required array b
print(*B)
# Test case 1
N = 4
arr1 = [2, 6, 1, 5]
# Function call
valid_array_formation(N, arr1)
# Test case 2
N = 6
arr2 = [2, 5, 3, 4, 6, 1]
# Function call
valid_array_formation(N, arr2)
C#
using System;
public class Program
{
// Function to generates valid array
// according to the given conditions
public static void ValidArrayFormation(int N, int[] arr)
{
int[] prefixXor = new int[N];
prefixXor[0] = arr[0];
// Calculating the prefix xor array
for (int i = 1; i < N; i++)
{
prefixXor[i] = prefixXor[i - 1] ^ arr[i];
}
int[] B = new int[N];
// Pointer to extract which element
// is to be extracted from array a
int pointer = 0;
int difference = 0;
for (int i = 0; i < N; i++)
{
// If difference becomes 2 then we
// increment the pointer
if (difference == 2)
{
pointer++;
difference = 0;
}
// B[i] i calculated accordingly
B[i] = prefixXor[i] ^ arr[pointer];
difference++;
}
// Printing the required array b
Console.WriteLine(string.Join(" ", B));
}
public static void Main()
{
// Test case 1
int N = 4;
int[] arr1 = new int[] { 2, 6, 1, 5 };
// Function call
ValidArrayFormation(N, arr1);
// Test case 2
N = 6;
int[] arr2 = new int[] { 2, 5, 3, 4, 6, 1 };
// Function call
ValidArrayFormation(N, arr2);
}
}
Output0 6 3 6
0 5 1 5 5 4
Time Complexity: O(N)
Auxiliary Space : O(N)
Similar Reads
Find an array of size N that satisfies the given conditions Given three integers N, S, and K, the task is to create an array of N positive integers such that the bitwise OR of any two consecutive elements from the array is odd and there are exactly K subarrays with a sum equal to S where 1 ? K ? N / 2. Examples: Input: N = 4, K = 2, S = 6 Output: 6 7 6 7 Her
8 min read
Generate an array B[] from the given array A[] which satisfies the given conditions Given an array A[] of N integers such that A[0] + A[1] + A[2] + ... A[N - 1] = 0. The task is to generate an array B[] such that B[i] is either ?A[i] / 2? or ?A[i] / 2? for all valid i and B[0] + B[1] + B[2] + ... + B[N - 1] = 0. Examples: Input: A[] = {1, 2, -5, 3, -1} Output: 0 1 -2 1 0Input: A[]
6 min read
Sum of Bitwise AND of sum of pairs and their Bitwise AND from a given array Given an array arr[] consisting of N integers, the task is to find the sum of Bitwise AND of (arr[i] + arr[j]) and Bitwise AND of arr[i] and arr[j] for each pair of elements (arr[i], arr[j]) from the given array. Since the sum can be very large, print it modulo (109 + 7). Examples: Input: arr[] = {8
9 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
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