Skip to content
Related Articles

Related Articles

Improve Article
Check if permutation of first N natural numbers exists having Bitwise AND of adjacent elements non-zero
  • Last Updated : 21 Apr, 2021

Given an integer N, the task is to check if there exists any permutation of the first N natural numbers [1, N] such that Bitwise AND of any pair of consecutive elements is not equal to 0. If any such permutation exists, print “Yes”. Otherwise, print “No”.

Examples: 

Input : 5
Output: Yes
Explanation: Permutation {2, 3, 1, 5, 4} satisfies the condition.

Input: 4
Output: No
Explanation: Since Bitwise AND of 4 and 3 is equal to 0, they cannot be placed adjacently. Similarly 2 and 4, 1 and 2, 1 and 4 cannot be placed adjacently. Therefore, no such permutation exists.

 

Approach: The problem can be solved based on the following observations: 



  • If N is a power of two, then N & (N – 1) is equal to 0. Therefore, no possible solution exists.
  • Otherwise, a permutation exists.

Therefore, to solve the problem, simply check if N is a power of 2 or not. If found to be false, print “Yes“. Otherwise, print “No“.

Below is the implementation of the above approach: 

C++




// C++ Program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a permutation
// of first N natural numbers exist
// with Bitwise AND of adjacent
// elements not equal to 0
void check(int n)
{
    // If n is a power of 2
    if ((n & n - 1) != 0)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}
 
// Driver Code
int main()
{
 
    int n = 5;
    check(n);
 
    return 0;
}

Java




// Java Program to implement
// the above approach
import java.util.*;
class solution{
     
// Function to check if a
// permutation of first N
// natural numbers exist
// with Bitwise AND of adjacent
// elements not equal to 0
static void check(int n)
{
  // If n is a power of 2
  if ((n & n - 1) != 0)
    System.out.println("YES");
  else
    System.out.println("NO");
}
 
// Driver Code
public static void main(String args[])
{
  int n = 5;
  check(n);
}
}
 
// This code is contributed by SURENDRA_GANGWAR

Python3




# Python3 program to implement
# the above approach
 
# Function to check if a permutation
# of first N natural numbers exist
# with Bitwise AND of adjacent
# elements not equal to 0
def check(n):
     
    # If n is a power of 2
    if ((n & n - 1) != 0):
        print("YES")
    else:
        print("NO")
 
# Driver Code
if __name__ == '__main__':
     
    n = 5
     
    check(n)
 
# This code is contributed by bgangwar59

C#




// C# Program to implement
// the above approach
using System;
class solution{
     
// Function to check if a
// permutation of first N
// natural numbers exist
// with Bitwise AND of adjacent
// elements not equal to 0
static void check(int n)
{
  // If n is a power of 2
  if ((n & n - 1) != 0)
    Console.WriteLine("YES");
  else
    Console.WriteLine("NO");
}
 
// Driver Code
public static void Main(String []args)
{
  int n = 5;
  check(n);
}
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
 
// Javascript program to implement
// the above approach
 
// Function to check if a
// permutation of first N
// natural numbers exist
// with Bitwise AND of adjacent
// elements not equal to 0
function check(n)
{
     
    // If n is a power of 2
    if ((n & n - 1) != 0)
        document.write("YES");
    else
        document.write("NO");
}
 
// Driver Code
var n = 5;
 
check(n);
 
// This code is contributed by umadevi9616
 
</script>
Output
YES

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :