Count Number of Pairs where Bitwise AND and Bitwise XOR is Equal Last Updated : 12 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: 0Explanation: There are no pairs satisfying the condition. Approach: This can be solved with the following idea: To make Bitwise XOR and Bitwise AND equal, it is only possible when both bits of first and second element are 0 at each bit place. Therefore, it boils down to calculate number of pairs possible where both elements are 0. Below are the steps involved: Initialize a count variable, count = 0.Iterate over array arr:if arr[i] == 0, increment in count by 1.To count number of pairs possible:(count * (count -1 ) ) / 2, will be the final ans.Below is the implementation of the above code: C++ // C++ Implementation #include <bits/stdc++.h> #include <iostream> using namespace std; // Function to count number of pairs // whose bitwise XOR and AND are equal int BitByBit(int arr[], int n) { int i = 0; int count = 0; // Iterate over array while (i < n) { // If arr[i] = 0 if (arr[i] == 0) { count++; } i++; } // Number of pairs return (count * (count - 1)) / 2; } // Driver code int main() { int n = 5; int arr[] = { 1, 0, 0, 2, 2 }; // Function call cout << BitByBit(arr, n); return 0; } Java public class BitwisePairs { // Function to count number of pairs // whose bitwise XOR and AND are equal static int bitByBit(int[] arr, int n) { int i = 0; int count = 0; // Iterate over array while (i < n) { // If arr[i] = 0 if (arr[i] == 0) { count++; } i++; } // Number of pairs return (count * (count - 1)) / 2; } // Driver code public static void main(String[] args) { int n = 5; int[] arr = {1, 0, 0, 2, 2}; // Function call System.out.println(bitByBit(arr, n)); } } Python3 # Python Implementation # Function to count number of pairs # whose bitwise XOR and AND are equal def BitByBit(arr, n): i = 0 count = 0 # Iterate over array while i < n: # If arr[i] = 0 if arr[i] == 0: count += 1 i += 1 # Number of pairs return (count * (count - 1)) // 2 # Driver code if __name__ == "__main__": n = 5 arr = [1, 0, 0, 2, 2] # Function call print(BitByBit(arr, n)) # This code is contributed by Sakshi C# using System; public class Solution { // Function to count number of pairs // whose bitwise XOR and AND are equal static int BitByBit(int[] arr, int n) { int i = 0; int count = 0; // Iterate over array while (i < n) { // If arr[i] = 0 if (arr[i] == 0) { count++; } i++; } // Number of pairs return (count * (count - 1)) / 2; } // Driver code public static void Main() { int n = 5; int[] arr = { 1, 0, 0, 2, 2 }; // Function call Console.WriteLine(BitByBit(arr, n)); } } // This code is contributed by akshitaguprzj3 JavaScript // JS Implementation // Function to count number of pairs // whose bitwise XOR and AND are equal function bitByBit(arr) { let count = 0; // Iterate over array for (let i = 0; i < arr.length; i++) { // If arr[i] = 0 if (arr[i] === 0) { count++; } } // Number of pairs return (count * (count - 1)) / 2; } // Driver code const arr = [1, 0, 0, 2, 2]; // Function call console.log(bitByBit(arr)); // This code is contributed by Sakshi Output1Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Count pairs with Bitwise XOR as EVEN number Anonymous Improve Article Tags : Bit Magic DSA Bitwise-XOR Data Structures Bitwise-AND Geeks Premier League 2023 +2 More Practice Tags : Bit MagicData Structures Similar Reads Count pairs with equal Bitwise AND and Bitwise OR value Given an array, arr[] of size N, the task is to count the number of unordered pairs such that Bitwise AND and Bitwise OR of each pair is equal. Examples: Input: arr[] = {1, 2, 1} Output: 1 Explanation: Bitwise AND value and Bitwise OR value all possible pairs are: Bitwise AND of the pair(arr[0], arr 6 min read Count pairs with Bitwise XOR as ODD number Given an array of N integers, the task is to find the number of pairs (i, j) such that A[i] ^ A[j] is odd.Examples: Input : N = 5 A[] = { 5, 4, 7, 2, 1} Output :6 Since pair of A[] = ( 5, 4 ) = 1( 5, 7 ) = 2( 5, 2 ) = 7( 5, 1 ) = 4 ( 4, 7 ) = 3( 4, 2 ) = 6( 4, 1 ) = 5 ( 7, 2 ) = 5( 7, 1 ) = 6 ( 2, 1 9 min read Count of pairs having bit size at most X and Bitwise OR equal to X Given a number X, calculate number of possible pairs (a, b) such that bitwise or of a and b is equal to X and number of bits in both a and b is less than equal to number of bits in X. Examples: Input: X = 6 Output: 9 Explanation: The possible pairs of (a, b) are (4, 6), (6, 4), (6, 6), (6, 2), (4, 2 4 min read Count of pairs with bitwise XOR value greater than its bitwise AND value Given an array arr that contains N positive Integers. Find the count of all possible pairs whose bitwise XOR value is greater than bitwise AND value Examples: Input : arr[]={ 12, 4, 15}Output: 2Explanation: 12 ^ 4 = 8, 12 & 4 = 4. so 12 ^ 4 > 12 & 4 4 ^ 15 = 11, 4 & 15 = 4. so 4 ^ 15 4 min read Count pairs with Bitwise XOR as EVEN number Given an array of N integers, the task is to find the number of pairs (i, j) such that A[i] ^ A[j] is even. Examples: Input: A[] = { 5, 4, 7, 2, 1} Output: 4 Since pair of A[] = ( 5, 4 ) = 1( 5, 7 ) = 2( 5, 2 ) = 7( 5, 1 ) = 4 ( 4, 7 ) = 3( 4, 2 ) = 6( 4, 1 ) = 5 ( 7, 2 ) = 5( 7, 1 ) = 6 ( 2, 1 ) = 11 min read Count pairs with Bitwise-AND as even number Given an array of N integers. The task is to find the number of pairs (i, j) such that A[i] & A[j] is even. Examples: Input: N = 4, A[] = { 5, 1, 3, 2 } Output: 3 Since pair of A[] are: ( 5, 1 ), ( 5, 3 ), ( 5, 2 ), ( 1, 3 ), ( 1, 2 ), ( 3, 2 ) 5 AND 1 = 1, 5 AND 3 = 1, 5 AND 2 = 0, 1 AND 3 = 1, 13 min read Like