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 = 5Output:3Explanation:

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 = 11Output:4Explanation:

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 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++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the first number` `int` `create_first_no(` `int` `n)` `{` ` ` `// Length of the binary from` ` ` `int` `length = 0;` ` ` `// Number of set bits` ` ` `int` `freq_set_bits = 0;` ` ` `int` `ans = 0;` ` ` `while` `(n) {` ` ` `// Update the first number` ` ` `ans = ans << 1;` ` ` `ans = ans + 1;` ` ` `// Increment length` ` ` `length++;` ` ` `// Update the frequency` ` ` `if` `((n & 1))` ` ` `freq_set_bits += 1;` ` ` `n = n >> 1;` ` ` `}` ` ` `// Check if n does not have all the` ` ` `// bits as set bits then make` ` ` `// the first as less than n` ` ` `if` `(length != freq_set_bits)` ` ` `ans = (ans >> 1);` ` ` `// Return the first value` ` ` `return` `ans;` `}` `// Function to calculate maximum` `// set bit frequency sum` `int` `maxSetBits(` `int` `n)` `{` ` ` `// First value of pair` ` ` `int` `first = create_first_no(n);` ` ` `// Second value of pair` ` ` `int` `second = n - first;` ` ` `// __builtin_popcount() is inbuilt` ` ` `// function to count the number of set bits` ` ` `int` `freq_first` ` ` `= __builtin_popcount(first);` ` ` `int` `freq_second` ` ` `= __builtin_popcount(second);` ` ` `// Return the sum of freq of setbits` ` ` `return` `freq_first + freq_second;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 5;` ` ` `// Function call` ` ` `cout << maxSetBits(N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement the` `// above approach` `import` `java.util.*;` `class` `GFG {` `// Function to find the first number` `static` `int` `create_first_no(` `int` `n)` `{` ` ` ` ` `// Length of the binary from` ` ` `int` `length = ` `0` `;` ` ` `// Number of set bits` ` ` `int` `freq_set_bits = ` `0` `;` ` ` `int` `ans = ` `0` `;` ` ` ` ` `while` `(n != ` `0` `) ` ` ` `{` ` ` `// Update the first number` ` ` `ans = ans << ` `1` `;` ` ` `ans = ans + ` `1` `;` ` ` `// Increment length` ` ` `length++;` ` ` `// Update the frequency` ` ` `if` `((n & ` `1` `) == ` `1` `)` ` ` `freq_set_bits += ` `1` `;` ` ` `n = n >> ` `1` `;` ` ` `}` ` ` ` ` `// Check if n does not have all the` ` ` `// bits as set bits then make` ` ` `// the first as less than n` ` ` `if` `(length != freq_set_bits)` ` ` `ans = (ans >> ` `1` `);` ` ` `// Return the first value` ` ` `return` `ans;` `}` `// Function to calculate maximum` `// set bit frequency sum` `static` `int` `maxSetBits(` `int` `n)` `{` ` ` ` ` `// First value of pair` ` ` `int` `first = create_first_no(n);` ` ` `// Second value of pair` ` ` `int` `second = n - first;` ` ` `// Integer.bitCount() is inbuilt` ` ` `// function to count the number of set bits` ` ` `int` `freq_first = Integer.bitCount(first);` ` ` `int` `freq_second = Integer.bitCount(second);` ` ` `// Return the sum of freq of setbits` ` ` `return` `freq_first + freq_second;` `}` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `5` `;` ` ` ` ` `// Function call` ` ` `System.out.println(maxSetBits(N));` `}` `}` `// This code is contributed by offbeat` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement the` `// above approach` `using` `System;` `using` `System.Linq; ` `class` `GFG {` ` ` `// Function to find the first number` `static` `int` `create_first_no(` `int` `n)` `{` ` ` ` ` `// Length of the binary from` ` ` `int` `length = 0;` ` ` ` ` `// Number of set bits` ` ` `int` `freq_set_bits = 0;` ` ` `int` `ans = 0;` ` ` ` ` `while` `(n != 0) ` ` ` `{` ` ` ` ` `// Update the first number` ` ` `ans = ans << 1;` ` ` `ans = ans + 1;` ` ` ` ` `// Increment length` ` ` `length++;` ` ` ` ` `// Update the frequency` ` ` `if` `((n & 1) == 1)` ` ` `freq_set_bits += 1;` ` ` ` ` `n = n >> 1;` ` ` `}` ` ` ` ` `// Check if n does not have all the` ` ` `// bits as set bits then make` ` ` `// the first as less than n` ` ` `if` `(length != freq_set_bits)` ` ` `ans = (ans >> 1);` ` ` ` ` `// Return the first value` ` ` `return` `ans;` `}` `public` `static` `int` `countSetBits(` `int` `n) ` `{ ` ` ` `// base case ` ` ` `if` `(n == 0) ` ` ` `return` `0; ` ` ` `else` ` ` `// if last bit set ` ` ` `// add 1 else add 0 ` ` ` `return` `(n & 1) + countSetBits(n >> 1); ` `} ` `// Function to calculate maximum` `// set bit frequency sum` `static` `int` `maxSetBits(` `int` `n)` `{` ` ` ` ` `// First value of pair` ` ` `int` `first = create_first_no(n);` ` ` ` ` `// Second value of pair` ` ` `int` `second = n - first;` ` ` ` ` `//countSetBits function to ` ` ` `//count the number of set bits` ` ` `int` `freq_first = countSetBits(first);` ` ` `int` `freq_second = countSetBits(second);` ` ` ` ` `// Return the sum of freq of setbits` ` ` `return` `freq_first + freq_second;` `}` ` ` `// Driver code` `public` `static` `void` `Main(` `string` `[] args)` `{` ` ` `int` `N = 5;` ` ` ` ` `// Function call` ` ` `Console.Write(maxSetBits(N));` `}` `}` ` ` `// This code is contributed by Ritik Bansal` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity: **O(logN)**Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count pairs from two arrays whose modulo operation yields K
- Count of valid pairs (X, Y) from given strings such that concatenating X with itself yields Y
- Sort elements of array whose modulo with K yields P
- Generate Array whose difference of each element with its left yields the given Array
- Sum of consecutive bit differences of first N non-negative integers
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Count of all possible pairs of disjoint subsets of integers from 1 to N
- Count of pairs of integers whose difference of squares is equal to N
- Sum of bit differences among all pairs
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Maximum set bit sum in array without considering adjacent elements
- Sum of all the prime numbers with the maximum position of set bit ≤ D
- Sum of f(a[i], a[j]) over all pairs in an array of n integers
- Inserting m into n such that m starts at bit j and ends at bit i.
- Find the maximum cost of an array of pairs choosing at most K pairs
- Maximum count of pairs which generate the same sum
- Maximum Count of pairs having equal Sum based on the given conditions
- Median in a stream of integers (running integers)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.