Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Count all pairs of an array which differ in K bits in C++
In this tutorial, we will be discussing a program to find the number of pairs of an array which differ in K bits.
For this we will be provided with an array and an integer K. Our task is to find the number of pairs who differ by K bits in their binary representation.
Example
#includeusing namespace std; //counting number of bits in //binary representation int count_bit(int n){ int count = 0; while (n) { if (n & 1) ++count; n >>= 1; } return count; } //counting the number of pairs long long count_pair(int arr[], int n, int k) { long long ans = 0; for (int i = 0; i Output
5
Advertisements
