Non-Repeating Bitwise OR Permutation Last Updated : 09 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an integer N (N >= 3). Then the task is to output a permutation such that the bitwise OR of the previous two elements is not equal to the current element, given that the previous two element exists. Note: If multiple permutation exists, just print any valid permutation. Examples: Input: N = 3Output: 1 3 2Explanation: First element: A[1] = 1. Two previous elements don't exist. Therefore, no need to check.Second element: A[2] = 3. Two previous elements don't exist. Therefore, no need to check.Third element: A[3] = 2. Two previous elements are 1 and 3. Bitwise OR of 1 and 3 is (1 | 3) = 3, which is not equal to 2. Thus, permutation satisfies the problem constraints. Input: N = 5Output: 2 1 5 3 4Explanation: First element: A[1] = 2. Two previous elements don't exist. Therefore, no need to check.Second element: A[2] = 1. Two previous elements don't exist. Therefore, no need to check.Third element: A[3] = 5. Two previous elements are 1 and 2. Bitwise OR of 1 and 2 is (1 | 2) = 3, which is not equal to 5.Fourth element: A[4] = 3. Two previous elements are 1 and 5. Bitwise OR of 1 and 5 is (1 | 5) = 5, which is not equal to 3.Fifth element: A[5] = 4. Two previous elements are 5 and 3. Bitwise OR of 5 and 3 is (5 | 3) = 7, which is not equal to 4. Approach: Implement the idea below to solve the problem The problem is observation based. It must be noted that if X, Y, Z are positive integers such that Z = (X | Y), Then Z >= max(X, Y). Therefore, If we output N numbers in decreasing order then there's no way our permutation can be wrong. Hence, it's our required answer. Steps taken to solve the problem: Run a loop reversely from i = N to i = 1 and output i.Code to implement the approach: C++ #include <iostream> using namespace std; void PrintPermutation(int N) { for (int i = N; i >= 1; i--) { cout << i << " "; } } int main() { // Input int N = 5; // Function call PrintPermutation(N); return 0; } Java /*package whatever //do not write package name here */ import java.io.*; class GFG { public static void main(String[] args) { // Input int N = 5; // Function call Print_permutation(N); } public static void Print_permutation(int N) { for (int i = N; i >= 1; i--) { System.out.print(i + " "); } } } Python3 # Python program for the above approach def print_permutation(N): for i in range(N, 0, -1): print(i, end=" ") # Input N = 5 # Function call print_permutation(N) # This code is contributed by Susobhan Akhuli C# using System; public class GFG { public static void Main(string[] args) { // Input int N = 5; // Function call PrintPermutation(N); } public static void PrintPermutation(int N) { for (int i = N; i >= 1; i--) { Console.Write(i + " "); } } } JavaScript <script> // Javascript program for the above approach // Function to print permutation function printPermutation(N) { for (let i = N; i >= 1; i--) { document.write(i + " "); } } // Input const N = 5; // Function call printPermutation(N); // This code is contributed by Susobhan Akhuli </script> Output5 4 3 2 1 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Print bitwise AND set of a number N P pradeep6036ymca Follow Improve Article Tags : Bit Magic Geeks Premier League DSA permutation Greedy Algorithms Bitwise-OR Geeks Premier League 2023 +3 More Practice Tags : Bit Magicpermutation Similar Reads Find the possible permutation of the bits of N Given an integer N, the task is to find whether the bits of N can be arranged in alternating manner i.e. either 0101... or 10101.... Assume that N is represented as a 32 bit integer.Examples: Input: N = 23 Output: No "00000000000000000000000000010111" is the binary representation of 23 and the requi 4 min read Count valid Bitwise operation combinations for Binary Strings Given an odd integer N (N >= 3), a binary string S of length N and another string O of length (N-1)/2. Here, each character in string O can be one of the following: '&', '|' or '^' representing Bitwise operations AND, OR, and XOR respectively, the task is to find the number of possible combin 8 min read Zeroing out Array with bitwise operations Given an array A of N elements. Your task is to output "YES" if you can make all the array elements zero by performing an infinite number of moves where in one move: Choose any two distinct indexes i and j from the array A. Evaluate the bitwise AND of A[i] & A[j]. (Let it be X)Replace A[i] with 6 min read Implementation of C++ Bitset using String Let's implement bitset in C++, such that following operations can be performed in stated time complexities : init(int size): initializes a bitset of size number of 0 bits.void fix(int pos): Change the bit at position pos to 1. No change if it was already 1.void unfix(int pos): Change the bit at posi 4 min read Print bitwise AND set of a number N Given a number N, print all the numbers which are a bitwise AND set of the binary representation of N. Bitwise AND set of a number N is all possible numbers x smaller than or equal N such that N & i is equal to x for some number i. Examples : Input : N = 5Output : 0, 1, 4, 5 Explanation: 0 & 8 min read Bitwise Hacks for Competitive Programming Prerequisite: It is recommended to refer Interesting facts about Bitwise Operators How to set a bit in the number 'num': If we want to set a bit at nth position in the number 'num', it can be done using the 'OR' operator( | ).  First, we left shift '1' to n position via (1<<n)Then, use the 'O 14 min read Like