Count total set bits in an array
Last Updated :
28 Mar, 2024
Given an array arr, the task is to count the total number of set bits in all numbers of that array arr.
Example:
Input: arr[] = {1, 2, 5, 7}
Output: 7
Explanation: Number of set bits in {1, 2, 5, 7} are {1, 1, 2, 3} respectively
Input: arr[] = {0, 4, 9, 8}
Output: 4
Approach: Follow the below steps to solve this problem:
- Create a variable cnt to store the answer and initialize it with 0.
- Traverse on each element of the array arr.
- Now for each element, say x, run a loop while it’s greater than 0.
- Extract the last bit of x using (x&1) and then right shift x by a single bit.
- Return cnt as the answer to this problem.
Below is the implementation of the above approach:
C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the total number of set bits
// in an array of integers
int totalSetBits(vector<int>& arr)
{
int cnt = 0;
for (auto x : arr) {
// While x is greater than 0
while (x > 0) {
// Adding last bit to cnt
cnt += (x & 1);
// Right shifting x by a single bit
x >>= 1;
}
}
return cnt;
}
// Driver Code
int main()
{
vector<int> arr = { 1, 2, 5, 7 };
cout << totalSetBits(arr);
}
Java
// Java code for the above approach
import java.util.*;
class GFG{
// Function to count the total number of set bits
// in an array of integers
static int totalSetBits(int[] arr)
{
int cnt = 0;
for (int x : arr) {
// While x is greater than 0
while (x > 0) {
// Adding last bit to cnt
cnt += (x & 1);
// Right shifting x by a single bit
x >>= 1;
}
}
return cnt;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 1, 2, 5, 7 };
System.out.print(totalSetBits(arr));
}
}
// This code is contributed by shikhasingrajput
C#
// C# code for the above approach
using System;
class GFG {
// Function to count the total number of set bits
// in an array of integers
static int totalSetBits(int[] arr)
{
int cnt = 0;
for (int x = 0; x < arr.Length; x++) {
// While x is greater than 0
while (arr[x] > 0) {
// Adding last bit to cnt
cnt += (arr[x] & 1);
// Right shifting x by a single bit
arr[x] >>= 1;
}
}
return cnt;
}
// Driver Code
public static void Main(string[] args)
{
int[] arr = { 1, 2, 5, 7 };
Console.WriteLine(totalSetBits(arr));
}
}
// This code is contributed by ukasp.
Javascript
<script>
// JavaScript code for the above approach
// Function to count the total number of set bits
// in an array of integers
function totalSetBits(arr)
{
let cnt = 0;
for(let x of arr)
{
// While x is greater than 0
while (x > 0)
{
// Adding last bit to cnt
cnt += (x & 1);
// Right shifting x by a single bit
x >>= 1;
}
}
return cnt;
}
// Driver Code
let arr = [ 1, 2, 5, 7 ];
document.write(totalSetBits(arr));
// This code is contributed by Potta Lokesh
</script>
Python3
# python code for the above approach
# Function to count the total number of set bits
# in an array of integers
def totalSetBits(arr):
cnt = 0
for x in arr:
# While x is greater than 0
while (x > 0):
# Adding last bit to cnt
cnt += (x & 1)
# Right shifting x by a single bit
x >>= 1
return cnt
# Driver Code
if __name__ == "__main__":
arr = [1, 2, 5, 7]
print(totalSetBits(arr))
# This code is contributed by rakeshsahni
Time Complexity: O(N)
Auxiliary Space: O(1)
Using bin:
- We use list comprehension to iterate through each element x in the array arr.
- For each element x, we convert it to a binary string using the built-in bin() function, then count the number of ‘1’s in the binary representation using the count() method.
- We sum up the counts of set bits for all elements in the array using the sum() function and return the total count.
Python3
# Function to count the total number of set bits in an array of integers
def total_set_bits(arr):
return sum(bin(x).count('1') for x in arr)
# Driver Code
if __name__ == "__main__":
arr = [1, 2, 5, 7]
print(total_set_bits(arr))
Time Complexity: O(Nâ‹…M), where N is the number of elements in the array and M is the maximum number of bits in any element.
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...