Generate permutation of [1, N] having bitwise XOR of adjacent differences as 0
Last Updated :
26 Sep, 2022
Given an integer N, the task is to generate a permutation from 1 to N such that the bitwise XOR of differences between adjacent elements is 0 i.e., | A[1]− A[2] | ^ | A[2]− A[3] | ^ . . . ^ | A[N −1] − A[N] | = 0, where |X – Y| represents absolute difference between X and Y.
Examples:
Input: N = 4
Output: 2 3 1 4
Explanation: |2 -3| ^ |3 -1| ^ |1-4| = 1 ^ 2 ^ 3 = 0
Input: N = 3
Output: 1 2 3
Approach: This problem can be solved based on the following observation:
- The XOR of even number of same elements is 0. So if odd number of elements are there (which implies even number of adjacent differences) then arrange them in a way such that the difference between any two adjacent elements is same.
- Else if N is even (which implies odd number of adjacent differences) then arrange the first four in such a way that the XOR of first three differences is 0. Then the remaining elements in the above mentioned away.
Follow the steps mentioned below to implement the above observation:
- If N is odd, arrange all the N elements in a sorted manner because the difference between any two adjacent elements will be 1 and the number of adjacent differences are even.
- If N is even:
- Keep 2, 3, 1, 4 as the first four elements because the 3 differences have XOR 0.
- Now start from 5 and print the remaining elements in sorted order, which will give the difference as 1 for all the remaining even number of differences.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > shuffleArray( int n)
{
vector< int > res;
if (n < 3)
cout << -1 << endl;
else if (n % 2 != 0) {
for ( int i = 1; i <= n; i++)
res.push_back(i);
}
else {
res = { 2, 3, 1, 4 };
for ( int i = 5; i <= n; i++)
res.push_back(i);
}
return res;
}
int main()
{
int N = 4;
vector< int > ans = shuffleArray(N);
for ( int x : ans)
cout << x << " " ;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static ArrayList<Integer> shuffleArray( int n)
{
ArrayList<Integer> res = new ArrayList<Integer>();
if (n < 3 )
System.out.println(- 1 );
else if (n % 2 != 0 ) {
for ( int i = 1 ; i <= n; i++)
res.add(i);
}
else {
res.clear();
res.add( 2 );
res.add( 3 );
res.add( 1 );
res.add( 4 );
for ( int i = 5 ; i <= n; i++)
res.add(i);
}
return res;
}
public static void main(String args[])
{
int N = 4 ;
ArrayList<Integer> ans = shuffleArray(N);
for ( int i = 0 ; i < ans.size(); i++)
System.out.print(ans.get(i) + " " );
}
}
|
Python3
def shuffleArray(n):
res = []
if (n < 3 ):
print ( - 1 )
elif (n % 2 ! = 0 ):
for i in range ( 1 , n):
res.append(i)
else :
res = [ 2 , 3 , 1 , 4 ]
for i in range ( 5 , n):
res.append(i)
return res
if __name__ = = '__main__' :
n = 4
res = shuffleArray(n)
for i in res:
print (i, end = ' ' )
|
C#
using System;
using System.Collections;
class GFG
{
static ArrayList shuffleArray( int n)
{
ArrayList res = new ArrayList();
if (n < 3)
Console.WriteLine(-1);
else if (n % 2 != 0) {
for ( int i = 1; i <= n; i++)
res.Add(i);
}
else {
res.Clear();
res.Add(2);
res.Add(3);
res.Add(1);
res.Add(4);
for ( int i = 5; i <= n; i++)
res.Add(i);
}
return res;
}
public static void Main()
{
int N = 4;
ArrayList ans = shuffleArray(N);
foreach ( int x in ans)
Console.Write(x + " " );
}
}
|
Javascript
<script>
function shuffleArray(n)
{
let res = [];
if (n < 3)
document.write(-1, "</br>" );
else if (n % 2 != 0) {
for (let i = 1; i <= n; i++)
res.push(i);
}
else {
res = [ 2, 3, 1, 4 ];
for (let i = 5; i <= n; i++)
res.push(i);
}
return res;
}
let N = 4;
let ans = shuffleArray(N);
for (let x of ans)
document.write(x, " " );
</script>
|
Time Complexity: O(N)
Auxiliary space: O(N) because it is using extra space for vector res
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...