Open In App
Related Articles

Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N

Improve Article
Improve
Save Article
Save
Like Article
Like

Given an integer N, the task is to find all non-negative pairs (A, B) such that the sum of Bitwise OR and Bitwise AND of A, B is equal to N, i.e., (A | B) + (A & B) = N.

Examples:

Input: N = 5
Output: (0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0)
Explanation: All possible pairs satisfying the necessary conditions: 

  1. (0 | 5) + (0 & 5) = 5 + 0 = 5
  2. (1 | 4) + (1 & 4) = 5 + 0 = 5
  3. (2 | 3) + (2 & 3) = 3 + 2 = 5
  4. (3 | 2) + (3 & 2) = 3 + 2 = 5
  5. (4 | 1) + (4 & 1) = 5 + 0 = 5
  6. (5 | 0) + (5 & 0) = 5 + 0 = 5

Input: N = 7
Output: (0, 7), (1, 6), (2, 5), (3, 4), (4, 3), (5, 2), (6, 1), (7, 0)
Explanation: All possible pairs satisfying the necessary conditions:  

  1. (0 | 7) + (0 & 7) = 7 + 0 =7
  2. (1 | 6) + (1 & 6) = 7 + 0 =7
  3. (2 | 5) + (2 & 5) = 7 + 0 =7
  4. (3 | 4) + (3 & 4) = 7 + 0 =7
  5. (4 | 3) + (4 & 3) = 7 + 0 =7
  6. (5 | 2) + (5 & 2) = 7 + 0 =7
  7. (6 | 1) + (6 & 1) = 7 + 0 = 7
  8. (7 | 0) + (7 & 0) = 7 + 0 = 7
 

Naive Approach: The simplest approach is to iterate over the range [0, N] and print those pairs (A, B) that satisfy the condition (A | B) + (A & B) = N

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

Efficient Approach: To optimize the above approach, the idea is based on the observation that all those non-negative pairs whose sum is equal to N satisfy the given condition. Therefore, iterate over the range [0, N] using the variable i and print the pair i and (N – i).

Below is the implementation of the above approach.

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print all pairs whose
// sum of Bitwise OR and AND is N
void findPairs(int N)
{
    // Iterate from i = 0 to N
    for (int i = 0; i <= N; i++) {
 
        // Print pairs (i, N - i)
        cout << "(" << i << ", "
             << N - i << "), ";
    }
}
 
// Driver Code
int main()
{
    int N = 5;
    findPairs(N);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
import java.lang.*;
 
class GFG{
     
// Function to print all pairs whose
// sum of Bitwise OR and AND is N
static void findPairs(int N)
{
     
    // Iterate from i = 0 to N
    for(int i = 0; i <= N; i++)
    {
         
        // Print pairs (i, N - i)  
        System.out.print( "(" + i + ", " +
                          (N - i) + "), ");
   }
}
 
// Driver code
public static void main(String[] args)
{
    int N = 5;
     
    findPairs(N);
}
}
 
// This code is contributed by ajaykr00kj


Python3




# Python3 program for the above approach
 
# Function to print all pairs whose
# sum of Bitwise OR and AND is N
def findPairs(N):
     
     # Iterate from i = 0 to N
     for i in range(0, N + 1):
         
        # Print pairs (i, N - i)
        print("(", i, ",",
              N - i, "), ", end = "")
 
# Driver code
if __name__ == "__main__":
     
    N = 5
     
    findPairs(N)
 
# This code is contributed by ajaykr00kj


C#




// C# program for the above approach
using System;
  
class GFG{
  
// Function to print all pairs whose
// sum of Bitwise OR and AND is N
static void findPairs(int N)
{
     
    // Iterate from i = 0 to N
    for(int i = 0; i <= N; i++)
    {
         
        // Print pairs (i, N - i)  
        Console.Write( "(" + i + ", " +
                        (N - i) + "), ");
   }
}
 
// Driver code
public static void Main()
{
    int N = 5;
      
    findPairs(N);
}
}
 
// This code is contributed by sanjoy_62


Javascript




<script>
// JavaScript program for the above approach
 
// Function to print all pairs whose
// sum of Bitwise OR and AND is N
function findPairs(N)
{
      
    // Iterate from i = 0 to N
    for(let i = 0; i <= N; i++)
    {
          
        // Print pairs (i, N - i) 
        document.write( "(" + i + ", " +
                          (N - i) + "), ");
   }
}
 
// Driver Code
  let N = 5;
  findPairs(N);
  
 // This code is contributed by avijitmondal1998.
</script>


Output: 

(0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0),

 

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


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 23 Apr, 2021
Like Article
Save Article
Similar Reads
Related Tutorials