Sum of array elements whose count of set bits are unique
Given an array arr[] consisting of N positive integers, the task is to find the sum of all array elements having a distinct count of set bits in the array.
Examples:
Input: arr[] = {8, 3, 7, 5, 3}
Output: 15
Explanation:
The count of set bits in each array of elements is:
- arr[0] = 8 = (1000)2, has 1 set bits.
- arr[1] = 3 = (11)2, has 2 set bits.
- arr[2] = 7 = (111)2, has 3 set bits.
- arr[3] = 5 = (101)2, has 2 set bits.
- arr[4] = 3 = (11)2, has 2 set bits.
Therefore, the number of array elements whose count of set bits are unique are 8 and 7. Therefore, the required sum = 8 + 7 = 15.
Input: arr[] = {4, 5, 3, 5, 3, 2}
Output: 0
Approach: The idea is to store the element with the corresponding count of set bits on a map, then find the sum of elements having a unique count of set bits. Follow the steps below to solve the problem:
- Initialize a variable, say sum to store the resultant sum of elements, and a Map, say M that stores the elements having a particular count of set bits.
- Traverse the array arr[] and store the element arr[i] according to the set bit count in a Map M.
- Now, traverse the map and if the frequency of any set bit count is 1, then add the corresponding value associated with it in the variable sum.
- After completing the above steps, print the value of the sum as the result.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int setBitCount( int n)
{
int ans = 0;
while (n)
{
ans += n & 1;
n >>= 1;
}
return ans;
}
int getSum( int *arr, int n)
{
map< int , int > mp;
int ans = 0;
for ( int i = 0; i < n; i++)
{
int key = setBitCount(arr[i]);
mp[key] += 1;
}
for ( int i = 0; i < n; i++)
{
int key = setBitCount(arr[i]);
if (mp[key] == 1)
ans += arr[i];
}
cout << ans;
}
int main()
{
int arr[5] = {8, 3, 7, 5, 3};
int n = sizeof (arr) / sizeof (arr[0]);
getSum(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int setBitCount( int n)
{
int ans = 0 ;
while (n != 0 )
{
ans += n & 1 ;
n >>= 1 ;
}
return ans;
}
static void getSum( int []arr, int n)
{
Map<Integer,Integer> mp = new HashMap<Integer,Integer>();
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
int key = setBitCount(arr[i]);
if (mp.containsKey(key))
mp.put(key,mp.get(key) + 1 );
else
mp.put(key, 1 );
}
for ( int i = 0 ; i < n; i++)
{
int key = setBitCount(arr[i]);
if (mp.containsKey(key) && mp.get(key) == 1 )
ans += arr[i];
}
System.out.print(ans);
}
public static void main(String args[])
{
int []arr = { 8 , 3 , 7 , 5 , 3 };
int n = arr.length;
getSum(arr, n);
}
}
|
Python3
def setBitCount(n):
ans = 0
while n:
ans + = n & 1
n >> = 1
return ans
def getSum(arr):
mp = {}
ans = 0
for i in arr:
key = setBitCount(i)
mp[key] = [ 0 , i]
for i in arr:
key = setBitCount(i)
mp[key][ 0 ] + = 1
for i in mp:
if mp[i][ 0 ] = = 1 :
ans + = mp[i][ 1 ]
print (ans)
arr = [ 8 , 3 , 7 , 5 , 3 ]
getSum(arr)
|
C#
using System;
using System.Collections.Generic;
class solution{
static int setBitCount( int n)
{
int ans = 0;
while (n>0)
{
ans += n & 1;
n >>= 1;
}
return ans;
}
static void getSum( int []arr, int n)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
int ans = 0;
for ( int i = 0; i < n; i++)
{
int key = setBitCount(arr[i]);
if (mp.ContainsKey(key))
mp[key] += 1;
else
mp[key] = 1;
}
for ( int i = 0; i < n; i++)
{
int key = setBitCount(arr[i]);
if (mp[key] == 1)
ans += arr[i];
}
Console.Write(ans);
}
public static void Main()
{
int []arr = {8, 3, 7, 5, 3};
int n = arr.Length;
getSum(arr, n);
}
}
|
Javascript
<script>
function setBitCount(n)
{
let ans = 0;
while (n != 0)
{
ans += n & 1;
n >>= 1;
}
return ans;
}
function getSum(arr, n)
{
let mp = new Map();
let ans = 0;
for (let i = 0; i < n; i++)
{
let key = setBitCount(arr[i]);
if (mp.has(key))
mp.set(key,mp.get(key) + 1);
else
mp.set(key, 1);
}
for (let i = 0; i < n; i++)
{
let key = setBitCount(arr[i]);
if (mp.has(key) && mp.get(key) == 1)
ans += arr[i];
}
document.write(ans);
}
let arr = [8, 3, 7, 5, 3];
let n = arr.length;
getSum(arr, n);
</script>
|
Time Complexity: O(N)
Space Complexity: O(N) as we are using a map to store the count of the distinct bits.
Last Updated :
29 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...