Given three integers **N**, **K** and **X**, the task is to construct an array of length **N**, in which **XOR** of all elements of each contiguous sub-array of length **K** is **X**.

**Examples:**

Input:N = 5, K = 1, X = 4

Output:4 4 4 4 4

Explanation:

Each subarray of length 1 has Xor value equal to 4.

Input:N = 5, K = 2, X = 4

Output:4 0 4 0 4

Explanation:

Each subarray of length 2 has Xor value equal to 4.

**Approach:**

To solve the problem mentioned above, we need to follow the steps given below:

- Bitwise-XOR of any number,
**X**with**0**is equal to the number itself. So, if we set the first element of the array**A**as X, and the next**K – 1**elements as**0**, then we will have the XOR of elements of first sub-array of length K, equal to X. - If we set
**A[K]**as**A[0]**, then we will have XOR(A[1], …, A[K]) = X. Similarly, if we set**A[K + 1]**as**A[1]**, then we will have XOR(A[2], …, A[K+1]) = X - Continuing in this manner, we can observe that the general formula can be described as below:

A[i] = X, when

i % K == 0

A[i] = 0, wheni % K != 0where i is in the range[0,N – 1]

Below is the implementation of the above approach:

## C++

`// C++ implementation to Create an array ` `// in which the XOR of all elements of ` `// each contiguous sub-array of ` `// length K is X ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to construct the array ` `void` `constructArray(` `int` `N, ` `int` `K, ` `int` `X) ` `{ ` ` ` ` ` `// Creating a vector of size K, ` ` ` `// initialised with 0 ` ` ` `vector<` `int` `> ans(K, 0); ` ` ` ` ` `// Initialising the first element ` ` ` `// with the given XOR ` ` ` `ans[0] = X; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; ++i) { ` ` ` `cout << ans[i % K] << ` `" "` `; ` ` ` `} ` ` ` ` ` `cout << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 5, K = 2, X = 4; ` ` ` ` ` `constructArray(N, K, X); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to create an array ` `// in which the XOR of all elements of ` `// each contiguous sub-array of ` `// length K is X ` `class` `GFG{ ` ` ` `// Function to construct the array ` `public` `static` `void` `constructArray(` `int` `N, ` `int` `K, ` ` ` `int` `X) ` `{ ` ` ` ` ` `// Creating an array of size K, ` ` ` `// initialised with 0 ` ` ` `int` `[] ans = ` `new` `int` `[K]; ` ` ` ` ` `// Initialising the first element ` ` ` `// with the given XOR ` ` ` `ans[` `0` `] = X; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < N; ++i) ` ` ` `{ ` ` ` `System.out.print(ans[i % K] + ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `5` `, K = ` `2` `, X = ` `4` `; ` ` ` ` ` `constructArray(N, K, X); ` `} ` `} ` ` ` `// This code is contributed by divyeshrabadiya07 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to create an array ` `# in which the XOR of all elements of ` `# each contiguous sub-array of ` `# length K is X ` ` ` `# Function to construct the array ` `def` `constructArray(N, K, X): ` ` ` ` ` `# Creating a list of size K, ` ` ` `# initialised with 0 ` ` ` `ans ` `=` `[] ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, K): ` ` ` `ans.append(` `0` `) ` ` ` ` ` `# Initialising the first element ` ` ` `# with the given XOR ` ` ` `ans[` `0` `] ` `=` `X ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, N): ` ` ` `print` `(ans[i ` `%` `K], end ` `=` `" "` `) ` ` ` `# Driver code ` `N ` `=` `5` `K ` `=` `2` `X ` `=` `4` ` ` `# Function call ` `constructArray(N, K, X) ` ` ` `# This code is contributed by ishayadav181` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to create an array ` `// in which the XOR of all elements of ` `// each contiguous sub-array of ` `// length K is X ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to construct the array ` `public` `static` `void` `constructArray(` `int` `N, ` `int` `K, ` ` ` `int` `X) ` `{ ` ` ` ` ` `// Creating an array of size K, ` ` ` `// initialised with 0 ` ` ` `int` `[] ans = ` `new` `int` `[K]; ` ` ` ` ` `// Initialising the first element ` ` ` `// with the given XOR ` ` ` `ans[0] = X; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; ++i) ` ` ` `{ ` ` ` `Console.Write(ans[i % K] + ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `int` `N = 5, K = 2, X = 4; ` ` ` ` ` `constructArray(N, K, X); ` `} ` `} ` ` ` `// This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

**Output:**

4 0 4 0 4

**Time Complexity:*** O(N)*

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.

## Recommended Posts:

- Maximum length L such that the sum of all subarrays of length L is less than K
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Number of subarrays such that XOR of one half is equal to the other
- Choose X such that (A xor X) + (B xor X) is minimized
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Count the number of subarrays having a given XOR
- Minimum absolute difference of XOR values of two subarrays
- Queries on XOR of XORs of all subarrays
- Maximize the number of subarrays with XOR as zero
- Sum of XOR of all subarrays
- Find number of subarrays with XOR value a power of 2
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Maximum of XOR of first and second maximum of all subarrays
- Count subarrays with sum equal to its XOR value
- Count of Subarrays in an array containing numbers from 1 to the length of subarray
- Count of possible subarrays and subsequences using given length of Array
- Generate a unique Array of length N with sum of all subarrays divisible by N
- Queries to find maximum sum contiguous subarrays of given length in a rotating array
- Construct a string of length L such that each substring of length X has exactly Y distinct letters

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.