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.
Input: 32 3 7 5 27 28
Input: 2 3 8 5 6 7
- 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:
Time Complexity: O(N * 32)
Auxiliary Space: O(N)
- Count number of bits changed after adding 1 to given N
- Smallest number whose set bits are maximum in a given range
- Alternate bits of two numbers to create a new number
- Closest (or Next) smaller and greater numbers with same number of set bits
- Print numbers having first and last bits as the only set bits
- Check if bits of a number has count of consecutive set bits in increasing order
- Toggle bits of a number except first and last bits
- Sum of numbers with exactly 2 bits set
- Print first n numbers with exactly two set bits
- Find element with the maximum set bits in an array
- Count total set bits in all numbers from 1 to N | Set 3
- Count total set bits in all numbers from 1 to n
- Count total set bits in all numbers from 1 to n | Set 2
- Maximum Sum of Products of two arrays by toggling adjacent bits
- Check if bits in range L to R of two numbers are complement of each other or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar