# Maximum number of contiguous array elements with same number of set bits in C++

C++Server Side ProgrammingProgramming

We are given with an unsorted array of integer elements and the task is to calculate the two major things i.e.

• Elements with same number of set bits
• Also, Elements with the same set bits should be contiguous in nature.

## Input

int arr[] = { 5, 8, 1, 2, 9, 12}

Output − Maximum number of contiguous array elements with same number of set bits are − 3

Explanation − we will calculate the binary digits for the elements of an array and calculate their set bits.

arr = 5 => 0101 => total set bits are -: 2
arr = 8 => 1000 => total set bits are -: 1
arr = 1 => 0001 => total set bits are -: 1
arr = 2 => 0010 => total set bits are -: 1
arr = 9 => 1001 => total set bits are -: 2
Arr = 12 => 1100 => total set bits are -: 2

So the elements with the same number of set bits and also contiguous in nature are 5, 9 and 12. So the maximum number of contiguous array elements with same number of set bits are 3

Input − int arr[] = { 5, 8, 1, 2}

Output − Maximum number of contiguous array elements with same number of set bits are − 2

Explanation − we will calculate the binary digits for the elements of an array and calculate their set bits.

arr = 5 => 0101 => total set bits are -: 2
arr = 8 => 1000 => total set bits are -: 1
arr = 1 => 0001 => total set bits are -: 1
arr = 2 => 0010 => total set bits are -: 1

So the elements with the same number of set bits and also contiguous in nature are 1 and 2. So the maximum number of contiguous array elements with same number of set bits are 2

## Approach used in the below program is as follows

• Input the array elements of integer type

• Calculate the size of an array using size function and pass it to the function

• Take a temporary variable and set it to value 1 and also a maximum variable with the value 1.

• Create a variable vec of type vector

• Start the loop for from 0 till the size of an array

• Calculate the binary set bits of array elements using “__builtin_popcount(element)” function that will return the total number of set bits of a given element passed to it and keep storing the count into the vector.

• Start loop for from 1 till size of a vector

• Inside the vector, check if vec[i+1] = vec[i] then increment the value of temp by 1

• Else, set temp to 1

• Set maximum with choosing the max value between temp and maximum using the max function.

• Return the maximum variable

• Print the result.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
//function to calculate maximum same number of bits
int maximum_SameBits(int arr[], int size){
int temp = 1;
int maximum = 1;
vector<int> vec;
for (int i = 0; i < size; i++){
vec.push_back(__builtin_popcount(arr[i]));
}
for (int i = 1; i < vec.size(); i++){
if (vec[i + 1] == vec[i]){
temp++;
}
else{
temp = 1;
}
maximum = max(maximum, temp);
}
return maximum;
}
int main(){
int arr[] = { 5, 8, 1, 2, 9, 12};
int size = sizeof(arr) / sizeof(arr);
cout<<"Maximum number of contiguous array elements with same number of set bits are:
"<<maximum_SameBits(arr, size);
return 0;
}

## Output

Maximum number of contiguous array elements with same number of set bits are: 3