- Related Questions & Answers
- Next higher number with same number of set bits in C++
- Maximum sum by adding numbers with same number of set bits in C++
- Number of integers with odd number of set bits in C++
- Check if a number has same number of set and unset bits in C++
- Program to find higher number with same number of set bits as n in Python?\n
- Minimum number using set bits of a given number in C++
- XOR of all elements of array with set bits equal to K in C++
- Prime Number of Set Bits in Binary Representation in C++
- Find next greater number with same set of digits in C++
- Find integers that divides maximum number of elements of the array in C++
- Maximum number with same digit factorial product in C++
- Maximum no. of contiguous Prime Numbers in an array in C++
- Find largest number smaller than N with same set of digits in C++
- Number of Subarrays with Bounded Maximum in C++
- Number of pairs with maximum sum in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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[0] = 5 => 0101 => total set bits are -: 2 arr[1] = 8 => 1000 => total set bits are -: 1 arr[2] = 1 => 0001 => total set bits are -: 1 arr[3] = 2 => 0010 => total set bits are -: 1 arr[4] = 9 => 1001 => total set bits are -: 2 Arr[5] = 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[0] = 5 => 0101 => total set bits are -: 2 arr[1] = 8 => 1000 => total set bits are -: 1 arr[2] = 1 => 0001 => total set bits are -: 1 arr[3] = 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

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.

#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[0]); cout<<"Maximum number of contiguous array elements with same number of set bits are: "<<maximum_SameBits(arr, size); return 0; }

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

Advertisements