Construct an array whose Prefix XOR array starting from X is an N-length increasing sequence Last Updated : 09 Dec, 2021 Comments Improve Suggest changes Like Article Like Report 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 = 3Output: [2, 3, 1, 7]Explanation: Prefix XOR array for the array {2, 3, 1, 7} is as follows: X ? 2 = 1. Now, X = 1X ? 3 = 2. Now, X = 2X ? 1 = 3. Now, X = 3X ? 7 = 4. Now, X = 4 The array [1, 2, 3, 4] is a permutation of first 4 natural numbers. Input: N = 7, X = 52Output: [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 = BOrB = 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++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to print the required array void GenerateArray(int N, int X) { int prev_xor = X; // Iterative from 1 to N for (int i = 1; i <= N; i++) { // Print the i-th element cout << (i ^ prev_xor); if (i != N) { cout << " "; } // Update prev_xor to i prev_xor = i; } } // Driver Code int main() { // Given Input int N = 4, X = 3; // Function Call cout << "The generated array is "; GenerateArray(N, X); return 0; } Java // Java program for the above approach class GFG { // Function to print the required array static void GenerateArray(int N, int X) { int prev_xor = X; // Iterative from 1 to N for (int i = 1; i <= N; i++) { // Print the i-th element System.out.print(i ^ prev_xor); if (i != N) { System.out.print(" "); } // Update prev_xor to i prev_xor = i; } } // Driver Code public static void main(String args[]) { // Given Input int N = 4, X = 3; // Function Call System.out.print("The generated array is "); GenerateArray(N, X); } } // This code is contributed by splevel62. Python3 # Python 3 program for the above approach # Function to print the required array def GenerateArray(N, X): prev_xor = X # Iterative from 1 to N for i in range(1,N+1,1): # Print the i-th element print(i ^ prev_xor,end="") if (i != N): print(" ",end="") # Update prev_xor to i prev_xor = i # Driver Code if __name__ == '__main__': # Given Input N = 4 X = 3 # Function Call print("The generated array is ",end="") GenerateArray(N, X) # This code is contributed by ipg2016107 C# // C# program for above approach using System; class GFG { // Function to print the required array static void GenerateArray(int N, int X) { int prev_xor = X; // Iterative from 1 to N for (int i = 1; i <= N; i++) { // Print the i-th element Console.Write(i ^ prev_xor); if (i != N) { Console.Write(" "); } // Update prev_xor to i prev_xor = i; } } // Driver Code static void Main() { // Given Input int N = 4, X = 3; // Function Call Console.Write("The generated array is "); GenerateArray(N, X); } } // This code is contributed by sanjoy_62. JavaScript <script> // JavaScript program for the above approach; // Function to print the required array function GenerateArray(N, X) { let prev_xor = X; // Iterative from 1 to N for(let i = 1; i <= N; i++) { // Print the i-th element document.write((i ^ prev_xor)); if (i != N) { document.write(" "); } // Update prev_xor to i prev_xor = i; } } // Driver Code // Given Input let N = 4, X = 3; // Function Call document.write("The generated array is "); GenerateArray(N, X); // This code is contributed by Potta Lokesh </script> Output: The generated array is 2 3 1 7 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Construct an array whose Prefix XOR array starting from X is an N-length increasing sequence K kartikmodi Follow Improve Article Tags : Bit Magic Pattern Searching DSA Arrays Practice Tags : ArraysBit MagicPattern Searching Similar Reads Construct an Array of Strings having Longest Common Prefix specified by the given Array Given an integer array arr[] of size N, the task is to construct an array consisting of N+1 strings of length N such that arr[i] is equal to the Longest Common Prefix of ith String and (i+1)th String. Examples: Input: arr[] = {1, 2, 3} Output: {"abb", "aab", "aaa", "aaa"} Explanation: Strings "abb" 6 min read Longest increasing sub-sequence formed by concatenating array to itself N times Given an array arr[] of size N, the task is to find the length of the longest increasing subsequence in the array formed by the concatenation of the arr[] to itself at the end N times.Examples: Input: arr[] = {3, 2, 1}, N = 3 Output: 3 Explanation: The array formed by the concatenation - {3, 2, 1, 3 5 min read Construct original array starting with K from an array of XOR of all elements except elements at same index Given an array A[] consisting of N integers and first element of the array B[] as K, the task is to construct the array B[] from A[] such that for any index i, A[i] is the Bitwise XOR of all the array elements of B[] except B[i]. Examples: Input: A[] = {13, 14, 10, 6}, K = 2Output: 2 1 5 9Explanatio 6 min read Construct an array from XOR of all elements of array except element at same index Given an array A[] having n positive elements. The task to create another array B[] such as B[i] is XOR of all elements of array A[] except A[i].Examples : Input : A[] = {2, 1, 5, 9} Output : B[] = {13, 14, 10, 6} Input : A[] = {2, 1, 3, 6} Output : B[] = {4, 7, 5, 0} Recommended PracticeXOR of all 5 min read Construct lexicographically smallest Binary array of size N with A 0s and X inversion count Given three numbers N, A, and X, the task is to construct the lexicographically smallest binary array of size N, containing A 0s and having an inversion count of X. Examples: Input: N=5, A=2, X=1Output: 0 1 0 1 1Explanation: The number of inversions in this array is 1(2nd and 3rd index). Input: N=5, 9 min read Longest increasing sequence by the boundary elements of an Array Given an array arr[] of length N with unique elements, the task is to find the length of the longest increasing subsequence that can be formed by the elements from either end of the array.Examples: Input: arr[] = {3, 5, 1, 4, 2} Output: 4 Explanation: The longest sequence is: {2, 3, 4, 5} Pick 2, Se 8 min read Longest increasing sequence possible by the boundary elements of an Array Given an array arr[] of length N consisting of positive integers, the task is to find the longest increasing subsequence that can be formed by the elements from either end of the array. Examples : Input: N=4 arr[] ={ 1, 4, 2, 3 }Output: 1 3 4Explanation: Append arr[0] to the sequence. Sequence = {1} 14 min read Check if it is possible to construct an Array of size N having sum as S and XOR value as X Given three numbers N, S and X the task is to find if it is possible to construct a sequence A of length N, where each A[i] >= 0 for 1<=i<=N and the sum of all numbers in a sequence is equal to S, and the bit-wise XOR of sequence equals to X. Examples: Input: N = 3, S = 10, X = 4 Output: Ye 10 min read Minimum increments or decrements required to convert a sorted array into a power sequence Given a sorted array arr[] consisting of N positive integers, the task is to minimize the total number of increments or decrements of each array element required to convert the given array into a power sequence of any arbitrary integer X. A sequence is called a power sequence of any integer X, if an 8 min read Find array whose elements are XOR of adjacent elements in given array Given an array arr[] consisting of N integers, the task is to re-construct an array arr[] such that the values in arr[] are obtained by doing XOR of the adjacent elements in the array. Print the array elements. Examples: Input: arr[ ] = {10, 11, 1, 2, 3} Output: 1 10 3 1 3 Explanation: At index 0, a 5 min read Like