Permutation of first N natural numbers having product of Bitwise AND of adjacent pairs exceeding 0
Last Updated :
11 Nov, 2021
Given a positive integer N, the task is to find the permutation of first N natural numbers such that the product of Bitwise AND(&) of pairs of adjacent elements is greater than 0. If no such permutation is found, then print “Not Possible”.
Examples:
Input: N = 3
Output: 1 3 2
Explanation:
1 & 3 = 1
3 & 2 = 2
Product of bitwise AND of adjacent elements = 1 * 2 = 2(>0)
Therefore, the required output is 1 3 2
Input: 2
Output: Not Possible
Explanation:
Possible permutations of first N(= 2) natural numbers are: { { 1, 2 }, { 2, 1 } }
1 & 2 = 0
2 & 1 = 0
Therefore, the required output is Not Possible.
Naive Approach: The simplest approach to solve this problem is to generate all possible permutations of the first N natural numbers. For every permutation check if the product of bitwise AND of its adjacent elements is greater than 0 or not. If found to be true, then print that permutation. Otherwise, print “Not Possible”.
Time Complexity: O(N * N!)
Auxiliary Space: O(1)
Efficient Approach: The problem can be solved based on the following observations:
If N is a power of 2, then the bitwise AND of N with any number less than N must be 0.
If bitwise AND of adjacent elements is equal to 0, then the product of bitwise AND of its adjacent element must be 0.
Follow the steps below to solve the problem:
- If N is a power of 2, then print “Not Possible”.
- Initialize an array say, arr[] to store the permutation of first N natural numbers that satisfy the given condition.
- Iterate over the range [1, N], and update arr[i] = i
- Update first three elements of the array such that bitwise AND of its adjacent elements must be greater than 0, i.e. arr[1] = 2, arr[2] = 3 and arr[3] = 1
- Iterate over the range [4, N]. For every ith value, check if i is a power of 2 or not. If found to be true, then swap(arr[i], arr[i+1]).
- Finally, print the arr[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPowerOfTwo( int n)
{
if (n == 0)
return false ;
return ( ceil (log2(n))
== floor (log2(n)));
}
void findThePermutation( int N)
{
if (N == 1) {
cout << "1" ;
return ;
}
if (isPowerOfTwo(N)) {
cout << "Not Possible" ;
return ;
}
int arr[N + 1];
for ( int i = 1; i <= N; i++) {
arr[i] = i;
}
arr[1] = 2, arr[2] = 3, arr[3] = 1;
for ( int i = 4; i <= N; i++) {
if (isPowerOfTwo(i)) {
swap(arr[i], arr[i + 1]);
i++;
}
}
for ( int i = 1; i <= N; i++)
cout << arr[i] << " " ;
}
int main()
{
int N = 5;
findThePermutation(N);
return 0;
}
|
Java
class GFG
{
public static int log2( int N)
{
int result = ( int )(Math.log(N) / Math.log( 2 ));
return result;
}
static boolean isPowerOfTwo( int n)
{
if (n == 0 )
return false ;
return Math.ceil(log2(n)) == Math.floor(log2(n));
}
static void findThePermutation( int N)
{
if (N == 1 )
{
System.out.print( "1" );
return ;
}
if (isPowerOfTwo(N) == false )
{
System.out.print( "Not Possible" );
return ;
}
int arr[] = new int [N + 1 ];
for ( int i = 1 ; i <= N; i++)
{
arr[i] = i;
}
arr[ 1 ] = 2 ; arr[ 2 ] = 3 ; arr[ 3 ] = 1 ;
int temp;
for ( int i = 4 ; i <= N; i++)
{
if (isPowerOfTwo(i) == true )
{
temp = arr[i];
arr[i] = arr[i + 1 ];
arr[i + 1 ] = temp ;
i++;
}
}
for ( int i = 1 ; i <= N; i++)
System.out.print(arr[i] + " " );
}
public static void main(String[] args)
{
int N = 5 ;
findThePermutation(N);
}
}
|
Python3
from math import ceil, floor, log2
def isPowerOfTwo(n):
if (n = = 0 ):
return False
return (ceil(log2(n)) = = floor(log2(n)))
def findThePermutation(N):
if (N = = 1 ):
print ( "1" )
return
if (isPowerOfTwo(N)):
print ( "Not Possible" )
return
arr = [i for i in range (N + 1 )]
for i in range ( 1 , N + 1 ):
arr[i] = i
arr[ 1 ], arr[ 2 ], arr[ 3 ] = 2 , 3 , 1
for i in range ( 4 , N + 1 ):
if (isPowerOfTwo(i)):
arr[i], arr[i + 1 ] = arr[i + 1 ], arr[i]
i + = 1
for i in range ( 1 , N + 1 ):
print (arr[i], end = " " )
if __name__ = = '__main__' :
N = 5
findThePermutation(N)
|
C#
using System;
class GFG
{
public static int log2( int N)
{
int result = ( int )(Math.Log(N) / Math.Log(2));
return result;
}
static bool isPowerOfTwo( int n)
{
if (n == 0)
return false ;
return Math.Ceiling(( double )log2(n)) ==
Math.Floor(( double )log2(n));
}
static void findThePermutation( int N)
{
if (N == 1)
{
Console.Write( "1" );
return ;
}
if (isPowerOfTwo(N) == false )
{
Console.Write( "Not Possible" );
return ;
}
int []arr = new int [N + 1];
for ( int i = 1; i <= N; i++)
{
arr[i] = i;
}
arr[1] = 2; arr[2] = 3; arr[3] = 1;
int temp;
for ( int i = 4; i <= N; i++)
{
if (isPowerOfTwo(i) == true )
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp ;
i++;
}
}
for ( int i = 1; i <= N; i++)
Console.Write(arr[i] + " " );
}
public static void Main(String[] args)
{
int N = 5;
findThePermutation(N);
}
}
|
Javascript
<script>
function log2(N) {
var result = parseInt( (Math.log(N) / Math.log(2)));
return result;
}
function isPowerOfTwo(n) {
if (n == 0)
return false ;
return Math.ceil(log2(n)) == Math.floor(log2(n));
}
function findThePermutation(N) {
if (N == 1) {
document.write( "1" );
return ;
}
if (isPowerOfTwo(N) == false ) {
document.write( "Not Possible" );
return ;
}
var arr = Array(N + 1).fill(0);
for (i = 1; i <= N; i++) {
arr[i] = i;
}
arr[1] = 2;
arr[2] = 3;
arr[3] = 1;
var temp;
for (i = 4; i <= N; i++) {
if (isPowerOfTwo(i) == true ) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
i++;
}
}
for (i = 1; i <= N; i++)
document.write(arr[i] + " " );
}
var N = 5;
findThePermutation(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...