- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Count pairs in an array such that both elements has equal set bits in C++

We are given an array of integer type elements and the task is to form the pairs from the given array and calculate the set bits of elements in the pair and check whether both the elements are having equal number of set bits or not.

Set bits in a binary number is represented by 1. Whenever we calculate the binary number of an integer value then it is formed as the combination of 0’s and 1’s. So, the digit 1 is known as set bit in the terms of the computer.

**Input**

int arr[] = {6, 5, 1, 3, 7}

**Output**

Count of pairs in an array such that both elements has equal set bits are: 3

**Explanation**

The pairs formed from the given array are-: (6, 5): 6 -> 2 set bits, 5 -> 2 set bits :(valid pair) (6, 5): 6 -> 2 set bits, 1 -> 1 set bit:(invalid pair) (6, 3): 6 -> 2 set bits, 3 -> 2 set bits :(valid pair) (6, 7): 6 -> 2 set bits, 7 -> 3 set bits :(invalid pair) (5, 1): 5 -> 2 set bits, 1 -> 1 set bits :(invalid pair) (5, 3): 5 -> 2 set bits, 3 -> 2 set bits :(valid pair) (5, 1): 5 -> 2 set bits, 7 -> 3 set bits :(invalid pair) (1, 3): 1 -> 1 set bits, 3 -> 2 set bits :(invalid pair) (1, 3): 1 -> 1 set bits, 7 -> 3 set bits :(invalid pair) (3, 7): 3 -> 2 set bits, 7 -> 3 set bits :(invalid pair) So, there are 3 valid pairs with equal number of set bits and those are (6, 5), (6, 3) and (5, 2)

**Input**

int arr[] = {4, 6, 3, 2}

**Output**

Count of pairs in an array such that both elements has equal set bits are: 3

**Explanation**

The pairs formed from the given array are-: (4, 6): 4 -> 1 set bits, 6 -> 2 set bits :(invalid pair) (4, 3): 4 -> 1 set bits, 3 -> 2 set bits :(invalid pair) (4, 2): 4 -> 1 set bits, 2 -> 1 set bits :(valid pair) (6, 3): 6 -> 2 set bits, 3 -> 2 set bits :(valid pair) (6, 2): 6 -> 2 set bits, 2 -> 1 set bits :(invalid pair) (3, 2): 3 -> 2 set bits, 2 -> 1 set bits :(invalid pair) So, there are 2 valid pairs with equal number of set bits and those are (4, 2) and (6, 3).

## Approach used in the below program is as follows

Input an array of integer elements and calculate the size of an array and pass the data to the function

Declare a temporary variable count to store the count of pairs with the equal number of set bits.

Start loop FOR from i to 0 till the size of an array

Inside the loop, start another loop FOR from j to i + 1 till the size of an array

Inside the loop, set first and second elements of a pair as the total number of set bits by calling the function ‘__builtin_popcount(element)’ that returns the total number of set bits of an integer number.

Check IF set bits of first and second element of a pair are equal then increment the count by 1

Return the count

Print result.

## Example

#include <iostream> using namespace std; int pair_setBit(int arr[], int size){ int count = 0; for(int i = 0 ;i <size ; i++){ for(int j = i+1; j<size; j++){ int first = __builtin_popcount(arr[i]); int second = __builtin_popcount(arr[j]); if(first == second){ count++; } } } return count; } int main(){ int arr[] = {6, 5, 1, 3, 7}; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of pairs in an array such that both elements has equal set bits are: "<<pair_setBit(arr, size); return 0; }

## Output

If we run the above code it will generate the following output −

Count of pairs in an array such that both elements has equal set bits are: 3

- Related Articles
- Count of index pairs with equal elements in an array in C++
- Count of pairs (x, y) in an array such that x < y in C++
- Count pairs in an array such that at least one element is prime in C++
- Count all pairs of an array which differ in K bits in C++
- Sort an array according to count of set bits in C++
- XOR of all elements of array with set bits equal to K in C++
- Count set bits in an integer in C++
- Count pairs in an array such that frequency of one is at least value of other in C++
- Count minimum bits to flip such that XOR of A and B equal to C in C++
- Count pairs (i,j) such that (i+j) is divisible by both A and B in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Python Program to Count set bits in an integer
- Java Program to Count set bits in an integer
- Check if bits of a number has count of consecutive set bits in increasing order in Python
- Count of pairs in an array that have consecutive numbers using JavaScript