Maximize total set bits of elements in N sized Array with sum M
Last Updated :
07 Jul, 2022
Given two integers N and M denoting the size of an array and the sum of the elements of the array, the task is to find the maximum possible count of total set bits of all the elements of the array such that the sum of the elements is M.
Examples:
Input: N = 1, M = 15
Output: 4
Explanation: Since N =1, 15 is the only possible solution.
The binary representation of 15 is (1111)2 which has 4 set bits.
Input: N = 2, M = 11
Output: 4
Explanation: There can be various options for the vector of size 2 and sum 11.
For example: [1, 10] this will give the set bits as 1 + 2 = 3
as their binary representations are 1 and 1010 respectively.
Similarly [2, 9] and [3, 8] will also give 3 set bits
as their binary representations are [10, 1001] and [11, 1000] respectively.
For [4, 7] the set bits will be maximum as
4 is represented by 100 and 7 is represented by 111.
So the total count of set bits = 1 + 3 =4.
Similarly [5, 6] is also a possible solution which gives sum of set bits 4.
For any other options the sum of set bits is always less than 4.
Hence the maximum number of set bits achieved is 4.
Approach: This problem can be solved by the concept of parity and greedy approach based on the following idea:
To maximize the set bit it is optimal to choose as small numbers as possible and set as many bits in each position as possible.
- Put as many 1s as feasible at the current bit for each bit from lowest to highest.
- However, if the parity of M and N differs, we won't be able to put N 1s on that bit since we won't be able to achieve M as the sum no matter how we set the upper bits.
- Hence we find out the minimum of N and M (say cur) and then compare the parity of cur and M.
If their parity is different we decrease the cur value by 1 to match the parity of M and set that many bits to 1.
Then adjust the sum M, by dividing it by 2 (For easy calculation in next step. We will only need to check the rightmost position and each set bit will contribute 1 to the sum) and repeat this till M becomes 0.
Follow the below illustration for a better understanding:
Illustration:
Consider N = 2 and M = 11.
1st Step:
=> Minimum of 2 and 11 = 2. So cur = 2
=> cur is even and M is odd. Decrease cur by 1. So cur = 2 - 1 = 1.
=> Set 1 bit. So, M = 10, ans = 1.
=> Change M = M/2 =
2nd Step:
=> Minimum of 2 and 5 = 2. So cur = 2
=> cur is even and M is odd. Decrease cur by 1. So cur = 2 - 1 = 1.
=> Set 1 bits. So, M = 5 - 1 = 4, ans = 1 + 1 = 2.
=> Set M = 4/2 = 2.
3rd Step:
=> Minimum of 2 and 2 = 2. So cur = 2
=> cur is even and M is also even.
=> Set 2 bits. So, M = 2 - 2 = 0, ans = 2 + 2 = 4.
=> Set M = 0/2 = 0.
Follow the below steps to implement the approach:
- Initialize variables to store minimum in each step and the answer.
- Iterate a loop till M is greater than 0:
- Find the minimum of M and N.
- Find the number of bits to be set using the above observation.
- Adjust the M accordingly as mentioned above.
- Add the count of bits set in this stage.
- At the end return the total count of bits as the required answer.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum possible set bits
// for an array of size n and sum of elements s
int maximizeSetBits(int n, int s)
{
int ans = 0;
// Condition for loop till s becomes 0
while (s) {
int cur = min(s, n);
// If parity is different
// some higher bit will be
// set in the next steps
// to achieve sum s
if ((cur % 2) != (s % 2)) {
// Decreasing the cur variable.
cur--;
}
// Updating the ans and sum respectively
ans += cur;
s = (s - cur) / 2;
}
// Return the maximum possible set bit
return ans;
}
// Driver code
int main()
{
int N = 2, M = 11;
// Function call
cout << maximizeSetBits(N, M);
return 0;
}
Java
// Java code to implement the approach
public class GFG {
// Function to find the maximum possible set bits
// for an array of size n and sum of elements s
static int maximizeSetBits(int n, int s)
{
int ans = 0;
// Condition for loop till s becomes 0
while (s != 0) {
int cur = Math.min(s, n);
// If parity is different
// some higher bit will be
// set in the next steps
// to achieve sum s
if ((cur % 2) != (s % 2)) {
// Decreasing the cur variable.
cur--;
}
// Updating the ans and sum respectively
ans += cur;
s = (s - cur) / 2;
}
// Return the maximum possible set bit
return ans;
}
// Driver code
public static void main (String[] args)
{
int N = 2, M = 11;
// Function call
System.out.println(maximizeSetBits(N, M));
}
}
// This code is contributed by AnkThon
Python3
# Python3 program to implement the approach
# Function to find the maximum possible set bits
# for an array of size n and sum of elements s
def maximizeSetBits(n, s):
ans = 0
# Condition for loop till s becomes 0
while s:
cur = min(s, n)
# If parity is different
# some higher bit will be
# set in the next steps
# to achieve sum s
if (cur % 2) != (s % 2):
# Decreasing the cur variable.
cur -= 1
# Updating the ans and sum respectively
ans += cur
s = (s - cur) // 2
# Return the maximum possible set bit
return ans
# Driver code
N, M = 2, 11
# Function call
print(maximizeSetBits(N, M))
# This code is contributed by phasing17
C#
// C# code to implement the approach
using System;
public class GFG
{
// Function to find the maximum possible set bits
// for an array of size n and sum of elements s
static int maximizeSetBits(int n, int s)
{
int ans = 0;
// Condition for loop till s becomes 0
while (s != 0) {
int cur = Math.Min(s, n);
// If parity is different
// some higher bit will be
// set in the next steps
// to achieve sum s
if ((cur % 2) != (s % 2)) {
// Decreasing the cur variable.
cur--;
}
// Updating the ans and sum respectively
ans += cur;
s = (s - cur) / 2;
}
// Return the maximum possible set bit
return ans;
}
// Driver code
public static void Main (string[] args)
{
int N = 2, M = 11;
// Function call
Console.WriteLine(maximizeSetBits(N, M));
}
}
// This code is contributed by AnkThon
JavaScript
<script>
// Function to find the maximum possible set bits
// for an array of size n and sum of elements s
function maximizeSetBits(n, s)
{
let ans = 0;
// Condition for loop till s becomes 0
while (s) {
let cur = Math.min(s, n);
// If parity is different
// some higher bit will be
// set in the next steps
// to achieve sum s
if ((cur % 2) != (s % 2)) {
// Decreasing the cur variable.
cur--;
}
// Updating the ans and sum respectively
ans += cur;
s = (s - cur) / 2;
}
// Return the maximum possible set bit
return ans;
}
// Driver code
let N = 2;
let M = 11;
// Function call
document.write(maximizeSetBits(N, M));
// This code is contributed by satwak4409.
</script>
Time Complexity: O(log M)
Auxiliary Space: O(1)
Similar Reads
Maximize sum of chosen Array elements with value at most M Given an array arr[] of N positive numbers and an integer M. The task is to maximize the value of M by adding array elements when arr[i] ⤠M. Note: Any array element can be added at most once. Examples: Input: arr[] = {3, 9, 19, 5, 21}, M = 10Output: 67Explanation: One way to getthe value isM > 3
4 min read
Find element with the maximum set bits in an array Given an array arr[]. The task is to find an element from arr[] which has the maximum count of set bits.Examples: Input: arr[] = {10, 100, 1000, 10000} Output: 1000 Binary(10) = 1010 (2 set bits) Binary(100) = 1100100 (3 set bits) Binary(1000) = 1111101000 (6 set bits) Binary(10000) = 10011100010000
5 min read
Count of N-size maximum sum Arrays with elements in range [0, 2^K â 1] and Bitwise AND equal to 0 Given two positive integers N and K, the task is to find the number of arrays of size N such that each array element lies over the range [0, 2K - 1] with the maximum sum of array element having Bitwise AND of all array elements 0. Examples: Input: N = 2 K = 2Output: 4Explanation:The possible arrays
5 min read
Rearrange an array to maximize sum of Bitwise AND of same-indexed elements with another array Given two arrays A[] and B[] of sizes N, the task is to find the maximum sum of Bitwise AND of same-indexed elements in the arrays A[] and B[] that can be obtained by rearranging the array B[] in any order. Examples: Input: A[] = {1, 2, 3, 4}, B[] = {3, 4, 1, 2}Output: 10Explanation: One possible wa
15 min read
Generate an Array such with elements maximized through swapping bits Given an array arr[], the task is to generate a modified array such that all its elements are maximized by swapping of bits.Examples: Input: arr[] = {10, 15} Output: 12, 15 Explanation: Binary representation of (10)10 = (1010)2. Swap the second and third bit to get the binary representation as (1100
6 min read