
- 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
Maximum number of contiguous array elements with same number of set bits in C++
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[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
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
#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; }
Output
Maximum number of contiguous array elements with same number of set bits are: 3
- Related Articles
- Maximum sum by adding numbers with same number of set bits in C++
- Next higher number with same number of set bits in C++
- Number of integers with odd number of set bits in C++
- Program to find higher number with same number of set bits as n in Python?\n
- Check if a number has same number of set and unset bits in C++
- 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++
- Prime Number of Set Bits in Binary Representation in Python
- Find integers that divides maximum number of elements of the array in C++
- Unpack elements and set the unpack count larger than the available number of bits in Numpy
- Count number of elements in an array with MongoDB?
- Find next greater number with same set of digits in C++
- Queries for number of array elements in a range with Kth Bit Set using C++
- Check if bits of a number has count of consecutive set bits in increasing order in Python
