Maximum sum by adding numbers with same number of set bits
Given an array of N numbers, the task is to find the maximum sum that can be obtained by adding numbers with the same number of set bits.
Examples:
Input: 32 3 7 5 27 28
Output: 34
Input: 2 3 8 5 6 7
Output: 14
Approach:
- Traverse in the array and count the number of set bits for every element.
- Initialize an array for 32 bits, assuming the number to have a maximum of 32 set bits.
- Iterate in the array and add the array element to the position which indicates the number of set bits.
- Traverse and find the maximum sum and return it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int bit_count( int n)
{
int count = 0;
while (n) {
count++;
n = n & (n - 1);
}
return count;
}
int maxsum( int arr[], int n)
{
int bits[n];
for ( int i = 0; i < n; i++) {
bits[i] = bit_count(arr[i]);
}
int sum[32] = { 0 };
for ( int i = 0; i < n; i++) {
sum[bits[i]] += arr[i];
}
int maximum = 0;
for ( int i = 0; i < 32; i++) {
maximum = max(sum[i], maximum);
}
return maximum;
}
int main()
{
int arr[] = { 2, 3, 8, 5, 6, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxsum(arr, n);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int max( int a, int b)
{
if (a>b)
return a;
else
return b;
}
int bit_count( int n)
{
int count = 0;
while (n) {
count++;
n = n & (n - 1);
}
return count;
}
int maxsum( int arr[], int n)
{
int bits[n];
for ( int i = 0; i < n; i++) {
bits[i] = bit_count(arr[i]);
}
int sum[32] = { 0 };
for ( int i = 0; i < n; i++) {
sum[bits[i]] += arr[i];
}
int maximum = 0;
for ( int i = 0; i < 32; i++) {
maximum = max(sum[i], maximum);
}
return maximum;
}
int main()
{
int arr[] = { 2, 3, 8, 5, 6, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
int ans=maxsum(arr,n);
printf ( "%d" ,ans);
return 0;
}
|
Java
class GFG
{
static int bit_count( int n)
{
int count = 0 ;
while (n> 0 )
{
count++;
n = n & (n - 1 );
}
return count;
}
static int maxsum( int [] arr, int n)
{
int [] bits= new int [n];
for ( int i = 0 ; i < n; i++)
{
bits[i] = bit_count(arr[i]);
}
int [] sum= new int [ 32 ];
for ( int i = 0 ; i < n; i++)
{
sum[bits[i]] += arr[i];
}
int maximum = 0 ;
for ( int i = 0 ; i < 32 ; i++)
{
maximum = Math.max(sum[i], maximum);
}
return maximum;
}
public static void main (String[] args)
{
int [] arr = { 2 , 3 , 8 , 5 , 6 , 7 };
int n = arr.length;
System.out.println(maxsum(arr, n));
}
}
|
Python3
def bit_count(n):
count = 0 ;
while (n > 0 ):
count + = 1 ;
n = n & (n - 1 );
return count;
def maxsum(arr, n):
bits = [ 0 ] * n;
for i in range (n):
bits[i] = bit_count(arr[i]);
sum = [ 0 ] * 32 ;
for i in range (n):
sum [bits[i]] + = arr[i];
maximum = 0 ;
for i in range ( 32 ):
maximum = max ( sum [i], maximum);
return maximum;
arr = [ 2 , 3 , 8 , 5 , 6 , 7 ];
n = len (arr);
print (maxsum(arr, n));
|
C#
using System;
class GFG
{
static int bit_count( int n)
{
int count = 0;
while (n > 0)
{
count++;
n = n & (n - 1);
}
return count;
}
static int maxsum( int [] arr, int n)
{
int [] bits = new int [n];
for ( int i = 0; i < n; i++)
{
bits[i] = bit_count(arr[i]);
}
int [] sum = new int [32];
for ( int i = 0; i < n; i++)
{
sum[bits[i]] += arr[i];
}
int maximum = 0;
for ( int i = 0; i < 32; i++)
{
maximum = Math.Max(sum[i], maximum);
}
return maximum;
}
static void Main()
{
int [] arr = { 2 ,3 , 8, 5, 6, 7 };
int n = arr.Length;
Console.WriteLine(maxsum(arr, n));
}
}
|
PHP
<?php
function bit_count( $n )
{
$count = 0;
while ( $n )
{
$count ++;
$n = $n & ( $n - 1);
}
return $count ;
}
function maxsum( $arr , $n )
{
$bits = array ( $n );
for ( $i = 0; $i < $n ; $i ++)
{
$bits [ $i ] = bit_count( $arr [ $i ]);
}
$sum = array_fill (0, 32, 0);
for ( $i = 0; $i < $n ; $i ++)
{
$sum [ $bits [ $i ]] += $arr [ $i ];
}
$maximum = 0;
for ( $i = 0; $i < 32; $i ++)
{
$maximum = max( $sum [ $i ],
$maximum );
}
return $maximum ;
}
$arr = array ( 2, 3, 8, 5, 6, 7 );
$n = sizeof( $arr );
echo maxsum( $arr , $n );
?>
|
Javascript
<script>
function bit_count(n)
{
var count = 0;
while (n) {
count++;
n = n & (n - 1);
}
return count;
}
function maxsum(arr, n)
{
var bits = Array(n);
for ( var i = 0; i < n; i++) {
bits[i] = bit_count(arr[i]);
}
var sum = Array(32).fill(0);
for ( var i = 0; i < n; i++) {
sum[bits[i]] += arr[i];
}
var maximum = 0;
for ( var i = 0; i < 32; i++) {
maximum = Math.max(sum[i], maximum);
}
return maximum;
}
var arr = [2, 3, 8, 5, 6, 7];
var n = arr.length;
document.write( maxsum(arr, n));
</script>
|
Time Complexity: O(N * 32)
Auxiliary Space: O(N)
Last Updated :
01 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...