Product of count of set bits present in binary representations of elements in an array
Last Updated :
20 May, 2021
Given an array arr[] consisting of N integers, the task is to find the product of the count of set bits in the binary representation of every array element.
Examples:
Input: arr[] = {3, 2, 4, 1, 5}
Output: 4
Explanation:
Binary representation of the array elements are {3, 2, 4, 1, 5} are {“11”, “10”, “100”, “1”, “101”} respectively.
Therefore, the product of count of set bits = (2 * 1 * 1 * 1 * 2) = 4.
Input: arr[] = {10, 11, 12}
Output: 12
Approach: The given problem can be solved by counting the total bits in the binary representation of every array element. Follow the steps below to solve the problem:
- Initialize a variable, say product, to store the resultant product.
- Traverse the given array arr[] and perform the following steps:
- After completing the above steps, print the value of the product as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countbits( int n)
{
int count = 0;
while (n != 0) {
if (n & 1)
count++;
n = n / 2;
}
return count;
}
int BitProduct( int arr[], int N)
{
int product = 1;
for ( int i = 0; i < N; i++) {
int bits = countbits(arr[i]);
product *= bits;
}
return product;
}
int main()
{
int arr[] = { 3, 2, 4, 1, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << BitProduct(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static int countbits( int n)
{
int count = 0 ;
while (n != 0 ) {
if ((n & 1 ) != 0 )
count++;
n = n / 2 ;
}
return count;
}
static int BitProduct( int arr[], int N)
{
int product = 1 ;
for ( int i = 0 ; i < N; i++) {
int bits = countbits(arr[i]);
product *= bits;
}
return product;
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 4 , 1 , 5 };
int N = arr.length;
System.out.print(BitProduct(arr, N));
}
}
|
Python3
def countbits(n):
count = 0
while (n ! = 0 ):
if (n & 1 ):
count + = 1
n = n / / 2
return count
def BitProduct(arr, N):
product = 1
for i in range (N):
bits = countbits(arr[i])
product * = bits
return product
if __name__ = = '__main__' :
arr = [ 3 , 2 , 4 , 1 , 5 ]
N = len (arr)
print (BitProduct(arr, N))
|
C#
using System;
public class GFG {
static int countbits( int n)
{
int count = 0;
while (n != 0) {
if ((n & 1) != 0)
count++;
n = n / 2;
}
return count;
}
static int BitProduct( int [] arr, int N)
{
int product = 1;
for ( int i = 0; i < N; i++) {
int bits = countbits(arr[i]);
product *= bits;
}
return product;
}
public static void Main( string [] args)
{
int [] arr = { 3, 2, 4, 1, 5 };
int N = arr.Length;
Console.Write(BitProduct(arr, N));
}
}
|
Javascript
<script>
function countbits( n)
{
let count = 0;
while (n != 0) {
if ((n & 1) != 0)
count++;
n = Math.floor(n / 2);
}
return count;
}
function BitProduct( arr, N)
{
let product = 1;
for (let i = 0; i < N; i++) {
let bits = countbits(arr[i]);
product *= bits;
}
return product;
}
let arr = [ 3, 2, 4, 1, 5 ];
let N = arr.length;
document.write(BitProduct(arr, N));
</script>
|
Time Complexity: O(N * log M), M is the maximum element of the array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...