Construct an array whose Prefix XOR array starting from X is an N | C ++ program for the above approach ; Function to print the required array ; Iteratie from 1 to N ; Print the i - th element ; Update prev_xor to i ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE 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 ; }