Given an integer array of length N (an arbitrarily large number). How to count number of set bits in the array?
The simple approach would be, create an efficient method to count set bits in a word (most prominent size, usually equal to bit length of processor), and add bits from individual elements of array.
Various methods of counting set bits of an integer exists, see this for example. These methods run at best O(logN) where N is number of bits. Note that on a processor N is fixed, count can be done in O(1) time on 32 bit machine irrespective of total set bits. Overall, the bits in array can be computed in O(n) time, where ‘n’ is array size.
However, a table look up will be more efficient method when array size is large. Storing table look up that can handle 232 integers will be impractical.
The following code illustrates simple program to count set bits in a randomly generated 64 K integer array. The idea is to generate a look up for first 256 numbers (one byte), and break every element of array at byte boundary. A meta program using C/C++ preprocessor generates the look up table for counting set bits in a byte.
The mathematical derivation behind meta program is evident from the following table (Add the column and row indices to get the number, then look into the table to get set bits in that number. For example, to get set bits in 10, it can be extracted from row named as 8 and column named as 2),
0, 1, 2, 3 0 - 0, 1, 1, 2 -------- GROUP_A(0) 4 - 1, 2, 2, 3 -------- GROUP_A(1) 8 - 1, 2, 2, 3 -------- GROUP_A(1) 12 - 2, 3, 3, 4 -------- GROUP_A(2) 16 - 1, 2, 2, 3 -------- GROUP_A(1) 20 - 2, 3, 3, 4 -------- GROUP_A(2) 24 - 2, 3, 3, 4 -------- GROUP_A(2) 28 - 3, 4, 4, 5 -------- GROUP_A(3) ... so on
From the table, there is a pattern emerging in multiples of 4, both in the table as well as in the group parameter. The sequence can be generalized as shown in the code.
All the operations takes O(1) except iterating over the array. The time complexity is O(n) where ‘n’ is size of array. Space complexity depends on the meta program that generates look up.
Contributed by Venki. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum number N such that total set bits of all numbers from 1 to N is at-least X
- Find the element having maximum set bits in the given range for Q queries
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Count distinct elements after adding each element of First Array with Second Array
- Count number of occurrences (or frequency) in a sorted array
- Number of ways to divide a given number as a set of integers in decreasing order
- Find the Rotation Count in Rotated Sorted array
- Count of smaller elements on right side of each element in an Array using Merge sort
- Count of unordered pairs (x, y) of Array which satisfy given equation
- Minimum count of elements required to obtain the given Array by repeated mirror operations
- Count number of pairs with the given Comparator
- Range Queries to count the number of even parity values with updates
- Find Second largest element in an array | Set 2
- Kth smallest element in the array using constant space when array can't be modified
- Maximum and minimum of an array using minimum number of comparisons
- Find closest number in array
- Find the Missing Number in a sorted array
- Largest perfect square number in an Array
- Find the number of different numbers in the array after applying the given operation q times
- Array range queries to find the maximum Fibonacci number with updates