Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N
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:
- (0 | 5) + (0 & 5) = 5 + 0 = 5
- (1 | 4) + (1 & 4) = 5 + 0 = 5
- (2 | 3) + (2 & 3) = 3 + 2 = 5
- (3 | 2) + (3 & 2) = 3 + 2 = 5
- (4 | 1) + (4 & 1) = 5 + 0 = 5
- (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:
- (0 | 7) + (0 & 7) = 7 + 0 =7
- (1 | 6) + (1 & 6) = 7 + 0 =7
- (2 | 5) + (2 & 5) = 7 + 0 =7
- (3 | 4) + (3 & 4) = 7 + 0 =7
- (4 | 3) + (4 & 3) = 7 + 0 =7
- (5 | 2) + (5 & 2) = 7 + 0 =7
- (6 | 1) + (6 & 1) = 7 + 0 = 7
- (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> |
(0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0),
Time Complexity: O(N)
Auxiliary Space: O(1)
Contact Us