Check if permutation of first N natural numbers exists having Bitwise AND of adjacent elements non-zero
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 0void check(int n){ // If n is a power of 2 if ((n & n - 1) != 0) cout << "YES" << endl; else cout << "NO" << endl;}// Driver Codeint main(){ int n = 5; check(n); return 0;} |
Java
// Java Program to implement// the above approachimport 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 0static 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 Codepublic 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 0def check(n): # If n is a power of 2 if ((n & n - 1) != 0): print("YES") else: print("NO")# Driver Codeif __name__ == '__main__': n = 5 check(n)# This code is contributed by bgangwar59 |
C#
// C# Program to implement// the above approachusing System;class solution{ // Function to check if a// permutation of first N// natural numbers exist// with Bitwise AND of adjacent// elements not equal to 0static void check(int n){ // If n is a power of 2 if ((n & n - 1) != 0) Console.WriteLine("YES"); else Console.WriteLine("NO");}// Driver Codepublic 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 0function check(n){ // If n is a power of 2 if ((n & n - 1) != 0) document.write("YES"); else document.write("NO");}// Driver Codevar n = 5;check(n);// This code is contributed by umadevi9616</script> |
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.


