Numbers whose bitwise OR and sum with N are equal
Last Updated : 12 Jun, 2022
Given a non-negative integer N, the task is to find count of non-negative integers
less than or equal to N whose bitwise OR and sum with N are equal.
Examples :
Input : N = 3 Output : 1 0 is the only number in [0, 3] that satisfies given property. (0 + 3) = (0 | 3) Input : 10 Output : 4 (0 + 10) = (0 | 10) (Both are 10) (1 + 10) = (1 | 10) (Both are 11) (4 + 10) = (4 | 10) (Both are 14) (5 + 10) = (5 | 10) (Both are 15)
A simple solution is to traverse all numbers from 0 to N and do bitwise OR and SUM with N, if both are equal increment counter.
Time complexity = O(N).
An efficient solution is to follow following steps.
1. Find count of zero bit in N.
2. Return pow(2,count).
The idea is based on the fact that bitwise OR and sum of a number x with N are equal, if and only if
bitwise AND of x with N will is 0
Let, N=10 =10102. Bitwise AND of a number with N will be 0, if number contains zero bit with all respective set bit(s) of N and either zero bit or set bit with all respective zero bit(s) of N (because, 0&0=1&0=0). e.g. bit : 1 0 1 0 position: 4 3 2 1 Bitwise AND of any number with N will be 0, if the number has following bit pattern 1st position can be either 0 or 1 (2 ways) 2nd position can be 1 (1 way) 3rd position can be either 0 or 1 (2 ways) 4th position can be 1 (1 way) Total count = 2*1*2*1 = 22 = 4.
C++ // C++ program to count numbers whose bitwise // OR and sum with N are equal #include <bits/stdc++.h> using namespace std; // Function to find total 0 bit in a number unsigned int CountZeroBit(int n) { unsigned int count = 0; while(n) { if (!(n & 1)) count++; n >>= 1; } return count; } // Function to find Count of non-negative numbers // less than or equal to N, whose bitwise OR and // SUM with N are equal. int CountORandSumEqual(int N ) { // count number of zero bit in N int count = CountZeroBit(N); // power of 2 to count return (1 << count); } // Driver code int main() { int N = 10; cout << CountORandSumEqual(N); return 0; }
Java // Java program to count numbers whose bitwise // OR and sum with N are equal class GFG { // Function to find total 0 bit in a number static int CountZeroBit(int n) { int count = 0; while(n > 0) { if ((n & 1) != 0) count++; n >>= 1; } return count; } // Function to find Count of non-negative // numbers less than or equal to N, whose // bitwise OR and SUM with N are equal. static int CountORandSumEqual(int N ) { // count number of zero bit in N int count = CountZeroBit(N); // power of 2 to count return (1 << count); } //Driver code public static void main (String[] args) { int N = 10; System.out.print(CountORandSumEqual(N)); } } // This code is contributed by Anant Agarwal.
Python3 # Python3 program to count numbers whose # bitwise OR and sum with N are equal # Function to find total 0 bit in a number def CountZeroBit(n): count = 0 while(n): if (not(n & 1)): count += 1 n >>= 1 return count # Function to find Count of non-negative # numbers less than or equal to N, whose # bitwise OR and SUM with N are equal. def CountORandSumEqual(N): # count number of zero bit in N count = CountZeroBit(N) # power of 2 to count return (1 << count) # Driver code N = 10 print(CountORandSumEqual(N)) # This code is contributed by Anant Agarwal.
C# // C# program to count numbers whose // bitwise OR and sum with N are equal using System; class GFG { // Function to find total // 0 bit in a number static int CountZeroBit(int n) { int count = 0; while(n>0) { if (n%2!=0) count++; n >>= 1; } return count; } // Function to find Count of non-negative // numbers less than or equal to N, whose // bitwise OR and SUM with N are equal. static int CountORandSumEqual(int N ) { // count number of zero bit in N int count = CountZeroBit(N); // power of 2 to count return (1 << count); } //Driver code public static void Main() { int N = 10; Console.Write(CountORandSumEqual(N)); } } // This code is contributed by Anant Agarwal.
PHP <?php // PHP program to count // numbers whose bitwise // OR and sum with N are equal // Function to find total // 0 bit in a number function CountZeroBit($n) { $count = 0; while($n) { if (!($n & 1)) $count++; $n >>= 1; } return $count; } // Function to find Count of // non-negative numbers less // than or equal to N, whose // bitwise OR and SUM with N // are equal. function CountORandSumEqual($N ) { // count number of // zero bit in N $count = CountZeroBit($N); // power of 2 to count return (1 << $count); } // Driver code $N = 10; echo CountORandSumEqual($N); // This code is contributed by Ajit ?>
JavaScript <script> // Javascript program to count numbers whose // bitwise OR and sum with N are equal // Function to find total // 0 bit in a number function CountZeroBit(n) { let count = 0; while(n>0) { if (n%2!=0) count++; n >>= 1; } return count; } // Function to find Count of non-negative // numbers less than or equal to N, whose // bitwise OR and SUM with N are equal. function CountORandSumEqual(N) { // count number of zero bit in N let count = CountZeroBit(N); // power of 2 to count return (1 << count); } let N = 10; document.write(CountORandSumEqual(N)); </script>
Output :
4
Total time complexity: O(log2(N))
Auxiliary Space: O(1)
Similar Reads
Count values whose Bitwise OR with A is equal to B Given two integers A and B, the task is to count possible values of X that satisfies the condition A | X = B. Note: | represents Bitwise OR operation. Examples: Input: A = 2, B = 3Output: 2Explanation: Since, 2 | 1 = 3 and 2 | 3 = 3. Therefore, the possible values of x are 1 and 3. Input: A = 5, B =
6 min read
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 = 5Output: (0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0)Explanation: All possible pairs satisfying the nec
5 min read
Number of unique pairs whose bitwise XOR and OR are same Given an integer N, the task is to find the number of pairs (say {a, b})from 1 to N (both inclusive) that satisfies the condition: a and b are distinct elements. The values of a | b and a ^ b are equal and a( a | b ) = b( a ^ b ) - ( a | b ) also holds true. Examples: Input: N=5Output:1Explanation:
9 min read
Count numbers less than N whose Bitwise AND with N is zero Given a positive integer N, the task is to count all numbers which are less than N, whose Bitwise AND of all such numbers with N is zero. Examples: Input: N = 5Output: 2Explanation: The integers less than N(= 5) whose Bitwise AND with 5 is 0 are 0 and 2. Hence, the total count is 2. Input: N = 9Outp
8 min read
Find a Number X whose sum with its digits is equal to N Given a positive number N. We need to find number(s) such that sum of digits of those numbers to themselves is equal to N. If no such number is possible print -1. Here N \in [1, 1000000000] Examples: Input : N = 21Output : X = 15Explanation : X + its digit sum = 15 + 1 + 5 = 21 Input : N = 5Output :
15+ min read
Count Number of Pairs where Bitwise AND and Bitwise XOR is Equal Given an integer array arr of size N, the task is to count the number of pairs whose BITWISE AND and BITWISE XOR are equal. Example: Input: N = 3, arr[] = {0,0,1}Output: 1Explanation: There is only one pair arr[0] and arr[1] as 0&0=0 and 0^0=0 Input: N = 4, arr[] = {1, 2, 4, 8}Output: 0Explanati
4 min read