Construct an array whose Prefix XOR array starting from X is an N-length increasing sequence
Last Updated :
09 Dec, 2021
Given two integers N and X, the task is to generate an array of size N, such that the prefix xor array of X with the generated array will be permutations of 1st N natural numbers.
Examples:
Input: N = 4, X = 3
Output: [2, 3, 1, 7]
Explanation: Prefix XOR array for the array {2, 3, 1, 7} is as follows:
- X ? 2 = 1. Now, X = 1
- X ? 3 = 2. Now, X = 2
- X ? 1 = 3. Now, X = 3
- X ? 7 = 4. Now, X = 4
The array [1, 2, 3, 4] is a permutation of first 4 natural numbers.
Input: N = 7, X = 52
Output: [53, 3, 1, 7, 1, 3, 1]
Approach: This problem can be solved using the properties of XOR ( If x ? a = b, then x ? b = a). Suppose XOR of elements in the generated array with X till ith index is X, and (i + 1)th element of the generated array is B, then B can be calculated using the following steps :
X ? B = i + 1
According to problem statement, using the property of XOR (if x? a = b then, x? b = a)…
X ? i + 1 = B
Or
B = X ? i + 1, which is the required value of B.
Follow the steps below to solve the problem:
- Initialize a variable, say prev_xor as X.
- Iterate a loop using a variable, say i, from 1 to N and print prev_xor ? i.
- Update prev_xor as i.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void GenerateArray( int N, int X)
{
int prev_xor = X;
for ( int i = 1; i <= N; i++) {
cout << (i ^ prev_xor);
if (i != N) {
cout << " " ;
}
prev_xor = i;
}
}
int main()
{
int N = 4, X = 3;
cout << "The generated array is " ;
GenerateArray(N, X);
return 0;
}
|
Java
class GFG {
static void GenerateArray( int N, int X)
{
int prev_xor = X;
for ( int i = 1 ; i <= N; i++) {
System.out.print(i ^ prev_xor);
if (i != N) {
System.out.print( " " );
}
prev_xor = i;
}
}
public static void main(String args[]) {
int N = 4 , X = 3 ;
System.out.print( "The generated array is " );
GenerateArray(N, X);
}
}
|
Python3
def GenerateArray(N, X):
prev_xor = X
for i in range ( 1 ,N + 1 , 1 ):
print (i ^ prev_xor,end = "")
if (i ! = N):
print ( " " ,end = "")
prev_xor = i
if __name__ = = '__main__' :
N = 4
X = 3
print ( "The generated array is " ,end = "")
GenerateArray(N, X)
|
C#
using System;
class GFG
{
static void GenerateArray( int N, int X)
{
int prev_xor = X;
for ( int i = 1; i <= N; i++) {
Console.Write(i ^ prev_xor);
if (i != N) {
Console.Write( " " );
}
prev_xor = i;
}
}
static void Main()
{
int N = 4, X = 3;
Console.Write( "The generated array is " );
GenerateArray(N, X);
}
}
|
Javascript
<script>
function GenerateArray(N, X)
{
let prev_xor = X;
for (let i = 1; i <= N; i++)
{
document.write((i ^ prev_xor));
if (i != N)
{
document.write( " " );
}
prev_xor = i;
}
}
let N = 4, X = 3;
document.write( "The generated array is " );
GenerateArray(N, X);
</script>
|
Output:
The generated array is 2 3 1 7
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...