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
Pairs from an array that satisfy the given condition
Given an array arr[], the task is to count all the valid pairs from the array. A pair (arr[i], arr[j]) is said to be valid if func( arr[i] ) + func( arr[j] ) = func( XOR(arr[i], arr[j]) ) where func(x) returns the number of set bits in x. Examples: Input: arr[] = {2, 3, 4, 5, 6} Output: 3 (2, 4), (2
9 min read
Count pairs having Bitwise XOR less than K from given array
Given an array arr[]of size N and an integer K, the task is to count the number of pairs from the given array such that the Bitwise XOR of each pair is less than K. Examples: Input: arr = {1, 2, 3, 5} , K = 5 Output: 4 Explanation: Bitwise XOR of all possible pairs that satisfy the given conditions
15+ min read
Count pairs in Array such that their bitwise XOR has Kth right bit set
Given an array arr[] of size N and an integer K, the task is to find the number of pairs in the array such that the Kth bit from the right of their bitwise XOR is set. Examples: Input: arr[] = {3, 13, 2, 9}, N = 4, K = 3Output: 4Explanation: The pairs that have the kth bit set in bitwise xor value a
11 min read
Count pairs having bitwise XOR greater than K from a given array
Given an array arr[]of size N and an integer K, the task is to count the number of pairs from the given array such that the Bitwise XOR of each pair is greater than K. Examples: Input: arr = {1, 2, 3, 5} , K = 2 Output: 4 Explanation: Bitwise XOR of all possible pairs that satisfy the given conditio
15+ 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
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
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
Count of pairs in Array such that bitwise AND of XOR of pair and X is 0
Given an array arr[] consisting of N positive integers and a positive integer X, the task is to find the number of pairs (i, j) such that i < j and (arr[i]^arr[j] )&X is 0. Examples: Input: arr[] = {1, 3, 4, 2}, X = 2 Output: 2Explanation:Following are the possible pairs from the given array:
11 min read