Given an array arr[] consisting of N integers, the task is to find the count of unordered pairs in the given array whose sum contains all set bits.
Examples:
Input: arr[] = {1, 2, 5}
Output: 2
Explanation: Possible pairs satisfying the conditions are:
- (1, 2): 1 + 2 = 3 (11) all bits are set in binary representation of 3.
- 2) (2, 5): 2 + 5 = 7 (111) all bits are set in binary representation of 7.
Therefore, the count is 2.
Input: arr[] = {1, 2, 5, 10}
Output: 3
Explanation: Possible pairs satisfying the conditions are:
- (1, 2): 1 + 2 = 3 (11) all bits are set in binary representation of 3.
- (2, 5): 2 + 5 = 7 (111) all bits are set in binary representation of 7.
- (5, 10): 5 + 10 = 15(1111) all bits are set in binary representation of 15.
Naive Approach: The idea is to generate all the possible pairs and check whether their sum has all bits set or not. If found to be true, then count that pair in the resultant count. Print the count after checking all the pairs.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to check if the number num// has all set bits or notbool allSetBits(int num){ // Find total bitsac int totalBits = log2(num) + 1; // Find count of set bit int setBits = __builtin_popcount(num); // Return true if all bit are set if (totalBits == setBits) return true; else return false;}// Function that find the count of// pairs whose sum has all set bitsint countPairs(int arr[], int n){ // Stores the count of pairs int ans = 0; // Generate all the pairs for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // Find the sum of current pair int sum = arr[i] + arr[j]; // If all bits are set if (allSetBits(sum)) ans++; } } // Return the final count return ans;}// Driver Codeint main(){ int arr[] = { 1, 2, 5, 10 }; int N = sizeof(arr) / sizeof(arr[0]); // Function Call cout << countPairs(arr, N); return 0;} |
Java
// Java program for the // above approachimport java.util.*;class GFG{// Function to check if the // number num has all set // bits or notstatic boolean allSetBits(int num){ // Find total bitsac int totalBits = (int)Math.log(num) + 1; // Find count of set bit int setBits = Integer.bitCount(num); // Return true if all // bit are set if (totalBits == setBits) return true; else return false;}// Function that find the // count of pairs whose sum // has all set bitsstatic int countPairs(int arr[], int n){ // Stores the count // of pairs int ans = 0; // Generate all the pairs for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // Find the sum of // current pair int sum = arr[i] + arr[j]; // If all bits are set if (allSetBits(sum)) ans++; } } // Return the final count return ans;}// Driver Codepublic static void main(String[] args){ int arr[] = {1, 2, 5, 10}; int N = arr.length; // Function Call System.out.print(countPairs(arr, N));}}// This code is contributed by gauravrajput1 |
Python3
# Python3 program for the above approachfrom math import log2# Function to check if the number num# has all set bits or notdef allSetBits(num): # Find total bits totalBits = int(log2(num) + 1) # Find count of set bit setBits = bin(num).count('1') # Return true if all bit are set if (totalBits == setBits): return True else: return False# Function that find the count of# pairs whose sum has all set bitsdef countPairs(arr, n): # Stores the count of pairs ans = 0 # Generate all the pairs for i in range(n): for j in range(i + 1, n): # Find the sum of current pair sum = arr[i] + arr[j] # If all bits are set if (allSetBits(sum)): ans += 1 # Return the final count return ans# Driver Codeif __name__ == '__main__': arr = [ 1, 2, 5, 10 ] N = len(arr) # Function Call print(countPairs(arr, N))# This code is contributed by mohit kumar 29 |
C#
// C# program for the // above approachusing System;class GFG{static int countSetBits(int n) { int count = 0; while (n > 0) { count += n & 1; n >>= 1; } return count; } // Function to check if the // number num has all set // bits or notstatic bool allSetBits(int num){ // Find total bitsac int totalBits = (int)Math.Log(num) + 1; // Find count of set bit int setBits = countSetBits(num); // Return true if all // bit are set if (totalBits == setBits) return true; else return false;}// Function that find the // count of pairs whose sum // has all set bitsstatic int countPairs(int []arr, int n){ // Stores the count // of pairs int ans = 0; // Generate all the pairs for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // Find the sum of // current pair int sum = arr[i] + arr[j]; // If all bits are set if (allSetBits(sum)) ans++; } } // Return the readonly count return ans;}// Driver Codepublic static void Main(String[] args){ int []arr = {1, 2, 5, 10}; int N = arr.Length; // Function Call Console.Write(countPairs(arr, N));}}// This code is contributed by Princi Singh |
3
Time Complexity: O(N2log N), where N is the size of the given array.
Auxiliary Space: O(N)
Efficient Approach: The key observation is that there are only log N numbers from 0 to N which contains all set bits. This property can be utilized to optimize the above approach. Follow the below steps to solve the problem:
- Store all the log(MAX_INTEGER) elements in an array setArray[].
- Map all the elements of the array arr[] in a Map data structure where an element is a key and its frequency is the value.
- Traverse the given array arr[] over the range [0, N – 1] and in nested loop traverse the array setArray[] from j = 0 to log(MAX_INTEGER) and increment ans by map[setArray[j] – arr[i]] where ans stores the total number of required pairs.
- As there is double counting because (a, b) and (b, a) are counted twice. Therefore, print the value of ans/2 to get the required count.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Store numbers having all set bitsvector<int> setArray;// Store frequency of values in arr[]map<int, int> mp;// Function to fill setArray[] with// numbers that have all set bitsvoid fillsetArray(){ for (int i = 1; i < 31; i++) { setArray.push_back((1 << i) - 1); }}// Function to find the count of pairs// whose sum contains all set bitsint countPairs(int arr[], int n){ // Stores the count of pairs int ans = 0; fillsetArray(); // Hash the values of arr[] in mp for (int i = 0; i < n; i++) mp[arr[i]]++; // Traverse the array arr[] for (int i = 0; i < n; i++) { // Iterate over the range [0, 30] for (int j = 0; j < 30; j++) { // Find the difference int value = setArray[j] - arr[i]; // Update the final count ans += mp[value]; } } // Return the final count return ans / 2;}// Driver Codeint main(){ int arr[] = { 1, 2, 5, 10 }; int N = sizeof(arr) / sizeof(arr[0]); // Function Call cout << countPairs(arr, N); return 0;} |
Java
// Java program for the // above approachimport java.util.*;class GFG{// Store numbers having// all set bitsstatic Vector<Integer> setArray = new Vector<>();// Store frequency of // values in arr[]static HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>(); // Function to fill setArray[] // with numbers that have all // set bitsstatic void fillsetArray(){ for (int i = 1; i < 31; i++) { setArray.add((1 << i) - 1); }}// Function to find the count // of pairs whose sum contains // all set bitsstatic int countPairs(int arr[], int n){ // Stores the count // of pairs int ans = 0; fillsetArray(); // Hash the values of // arr[] in mp for (int i = 0; i < n; i++) if(mp.containsKey(arr[i])) { mp.put(arr[i], mp.get(arr[i]) + 1); } else { mp.put(arr[i], 1); } // Traverse the array arr[] for (int i = 0; i < n; i++) { // Iterate over the range // [0, 30] for (int j = 0; j < 30; j++) { // Find the difference int value = setArray.get(j) - arr[i]; // Update the final count if(mp.containsKey(value)) ans += mp.get(value); } } // Return the final count return ans / 2;}// Driver Codepublic static void main(String[] args){ int arr[] = {1, 2, 5, 10}; int N = arr.length; // Function Call System.out.print(countPairs(arr, N));}}// This code is contributed by Rajput-Ji |
Python3
# Python3 program for the # above approachfrom collections import defaultdict# Store numbers having # all set bitssetArray = []# Store frequency of values # in arr[]mp = defaultdict (int)# Function to fill setArray[] with# numbers that have all set bitsdef fillsetArray(): for i in range (1, 31): setArray.append((1 << i) - 1) # Function to find the # count of pairs whose sum # contains all set bitsdef countPairs(arr, n): # Stores the count of pairs ans = 0 fillsetArray() # Hash the values of # arr[] in mp for i in range (n): mp[arr[i]] += 1 # Traverse the array arr[] for i in range (n): # Iterate over the range # [0, 30] for j in range (30): # Find the difference value = setArray[j] - arr[i] # Update the final count ans += mp[value] # Return the final count return ans // 2# Driver Codeif __name__ == "__main__": arr = [1, 2, 5, 10] N = len(arr) # Function Call print (countPairs(arr, N))# This code is contributed by Chitranayal |
C#
// C# program for the // above approachusing System;using System.Collections.Generic;class GFG{// Store numbers having// all set bitsstatic List<int> setArray = new List<int>();// Store frequency of // values in []arrstatic Dictionary<int, int> mp = new Dictionary<int, int>(); // Function to fill setArray[] // with numbers that have all // set bitsstatic void fillsetArray(){ for(int i = 1; i < 31; i++) { setArray.Add((1 << i) - 1); }}// Function to find the count // of pairs whose sum contains // all set bitsstatic int countPairs(int []arr, int n){ // Stores the count // of pairs int ans = 0; fillsetArray(); // Hash the values of // []arr in mp for(int i = 0; i < n; i++) if(mp.ContainsKey(arr[i])) { mp.Add(arr[i], mp[arr[i]] + 1); } else { mp.Add(arr[i], 1); } // Traverse the array []arr for(int i = 0; i < n; i++) { // Iterate over the range // [0, 30] for(int j = 0; j < 30; j++) { // Find the difference int value = setArray[j] - arr[i]; // Update the readonly count if (mp.ContainsKey(value)) ans += mp[value]; } } // Return the readonly count return ans / 2;}// Driver Codepublic static void Main(String[] args){ int []arr = {1, 2, 5, 10}; int N = arr.Length; // Function Call Console.Write(countPairs(arr, N));}}// This code is contributed by Princi Singh |
3
Time Complexity: O(N*32), where N is the size of the given array.
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Modify array by merging elements with addition such that it consists of only Primes.
- Print numbers having first and last bits as the only set bits
- Count Numbers with N digits which consists of odd number of 0's
- Count Numbers with N digits which consists of even number of 0’s
- Count of subsequences which consists exactly K prime numbers
- Check if a Binary Tree consists of a pair of leaf nodes with sum K
- Check if bits of a number has count of consecutive set bits in increasing order
- Count pairs with set bits sum equal to K
- Rearrange a given list such that it consists of alternating minimum maximum elements
- Smallest multiple of 3 which consists of three given non-zero digits
- Smallest integer greater than n such that it consists of digit m exactly k times
- Check whether the number has only first and last bits set | Set 2
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Count of numbers whose 0th and Nth bits are set
- Count of elements in an Array whose set bits are in a multiple of K
- Count subarrays consisting of only 0's and only 1's in a binary array
- Find the node whose sum with X has maximum set bits
- Find the node whose sum with X has minimum set bits
- Count pairs in an array such that both elements has equal set bits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

