Maximum set bit count from pairs of integers from 0 to N that yields a sum as N
Last Updated :
10 May, 2021
Given an integer N, the task is to find the maximum frequency of set bits among all pairs of integers from 0 to N that yields a sum as N.
Examples:
Input: N = 5
Output: 3
Explanation:
All the pairs are {0, 5}, {1, 4}, {2, 3} which has a sum as 5.
0 (0000) and 5 (0101), number of set bit = 2
1 (0001) and 4 (0100), number of set bit = 2
2 (0010) and 3 (0011), number of set bit = 3, hence 3 is the maximum.
Input: N = 11
Output: 4
Explanation:
All the pairs are {0, 11}, {1, 10}, {2, 9}, {3, 8}, {4, 7}, {5, 6} and the maximum ans will be for the pair {4, 7}
4 = 1000 and 7 = 0111, total number of set bits=1+3=4
Naive Approach: The simplest way to solve this problem is to generate all possible pairs with sum N and compute the maximum sum of the set bits of all such pairs, and print the maximum no of set bits sum.
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized through these steps:
- Find a number less than equal to N whose all the bits from the Least significant bit to the Most significant bit are set bits. That number will be the first number in the pair.
- Compute the number of set bits the pair {first, N-first} and sum it up.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int create_first_no( int n)
{
int length = 0;
int freq_set_bits = 0;
int ans = 0;
while (n) {
ans = ans << 1;
ans = ans + 1;
length++;
if ((n & 1))
freq_set_bits += 1;
n = n >> 1;
}
if (length != freq_set_bits)
ans = (ans >> 1);
return ans;
}
int maxSetBits( int n)
{
int first = create_first_no(n);
int second = n - first;
int freq_first
= __builtin_popcount(first);
int freq_second
= __builtin_popcount(second);
return freq_first + freq_second;
}
int main()
{
int N = 5;
cout << maxSetBits(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int create_first_no( int n)
{
int length = 0 ;
int freq_set_bits = 0 ;
int ans = 0 ;
while (n != 0 )
{
ans = ans << 1 ;
ans = ans + 1 ;
length++;
if ((n & 1 ) == 1 )
freq_set_bits += 1 ;
n = n >> 1 ;
}
if (length != freq_set_bits)
ans = (ans >> 1 );
return ans;
}
static int maxSetBits( int n)
{
int first = create_first_no(n);
int second = n - first;
int freq_first = Integer.bitCount(first);
int freq_second = Integer.bitCount(second);
return freq_first + freq_second;
}
public static void main(String[] args)
{
int N = 5 ;
System.out.println(maxSetBits(N));
}
}
|
Python3
def create_first_no(n):
length = 0
freq_set_bits = 0
ans = 0
while (n ! = 0 ):
ans = ans << 1
ans = ans + 1
length + = 1
if ((n & 1 ) ! = 0 ):
freq_set_bits + = 1
n = n >> 1
if (length ! = freq_set_bits):
ans = (ans >> 1 )
return ans
def maxSetBits(n):
first = create_first_no(n)
second = n - first
freq_first = bin (first).count( '1' )
freq_second = bin (second).count( '1' )
return (freq_first +
freq_second)
N = 5
print (maxSetBits(N))
|
C#
using System;
using System.Linq;
class GFG {
static int create_first_no( int n)
{
int length = 0;
int freq_set_bits = 0;
int ans = 0;
while (n != 0)
{
ans = ans << 1;
ans = ans + 1;
length++;
if ((n & 1) == 1)
freq_set_bits += 1;
n = n >> 1;
}
if (length != freq_set_bits)
ans = (ans >> 1);
return ans;
}
public static int countSetBits( int n)
{
if (n == 0)
return 0;
else
return (n & 1) + countSetBits(n >> 1);
}
static int maxSetBits( int n)
{
int first = create_first_no(n);
int second = n - first;
int freq_first = countSetBits(first);
int freq_second = countSetBits(second);
return freq_first + freq_second;
}
public static void Main( string [] args)
{
int N = 5;
Console.Write(maxSetBits(N));
}
}
|
Javascript
<script>
function create_first_no(n)
{
let length = 0;
let freq_set_bits = 0;
let ans = 0;
while (n != 0)
{
ans = ans << 1;
ans = ans + 1;
length++;
if ((n & 1) == 1)
freq_set_bits += 1;
n = n >> 1;
}
if (length != freq_set_bits)
ans = (ans >> 1);
return ans;
}
function countSetBits(n)
{
if (n == 0)
return 0;
else
return (n & 1) + countSetBits(n >> 1);
}
function maxSetBits(n)
{
let first = create_first_no(n);
let second = n - first;
let freq_first = countSetBits(first);
let freq_second = countSetBits(second);
return freq_first + freq_second;
}
let N = 5;
document.write(maxSetBits(N));
</script>
|
Time Complexity: O(logN)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...