 GeeksforGeeks App
Open App Browser
Continue

# Maximum set of number from the first N natural numbers whose Bitwise AND is positive

Given a positive integer N, the task is to find the maximum set of numbers from the first N natural numbers whose Bitwise AND is positive

Examples:

Input: N = 7
Output: 4
Explanation:
The set of numbers from the first N(= 7) natural numbers whose Bitwise AND is positive is {4, 5, 6, 7}, which is of maximum length.

Input: N = 16
Output: 8

Approach: The given problem can be solved based on the observation that 2N and (2N – 1), results in 0. Therefore, the maximum length of the set must not include both 2N and (2N – 1) in the same set. The maximum subarray with non-zero AND value can be found out as:

• Find the maximum power of 2 less than or equal to N and if N is a power of 2, the answer should be N/2, for example, when N is 16, the maximum subarray with non-zero AND value is 8.
• Otherwise, the answer is the length between the N and the largest power of 2 less than or equal to N.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find the maximum set of``// number whose Bitwise AND is positive``int` `maximumSizeOfSet(``int` `N)``{``    ``// Base Case``    ``if` `(N == 1)``        ``return` `1;` `    ``// Store the power of 2 less than``    ``// or equal to N``    ``int` `temp = 1;``    ``while` `(temp * 2 <= N) {``        ``temp *= 2;``    ``}` `    ``// N is power of 2``    ``if` `(N & (N - 1) == 0)``        ``return` `N / 2;``    ``else``        ``return` `N - temp + 1;``}` `// Driver Code``int` `main()``{``    ``int` `N = 7;``    ``cout << maximumSizeOfSet(N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``class` `GFG{` `// Function to find the maximum set of``// number whose Bitwise AND is positive``public` `static` `int` `maximumSizeOfSet(``int` `N)``{``  ` `    ``// Base Case``    ``if` `(N == ``1``)``        ``return` `1``;` `    ``// Store the power of 2 less than``    ``// or equal to N``    ``int` `temp = ``1``;``    ``while` `(temp * ``2` `<= N) {``        ``temp *= ``2``;``    ``}` `    ``// N is power of 2``    ``if` `((N & (N - ``1``)) == ``0``)``        ``return` `N / ``2``;``    ``else``        ``return` `N - temp + ``1``;``}` `// Driver Code``public` `static` `void` `main(String args[])``{``    ``int` `N = ``7``;``    ``System.out.println(maximumSizeOfSet(N));``}` `}` `// This code is contributed by gfgking.`

## Python3

 `# python program for the above approach` `# Function to find the maximum set of``# number whose Bitwise AND is positive``def` `maximumSizeOfSet(N):` `    ``# Base Case``    ``if` `(N ``=``=` `1``):``        ``return` `1` `    ``# Store the power of 2 less than``    ``# or equal to N``    ``temp ``=` `1``    ``while` `(temp ``*` `2` `<``=` `N):``        ``temp ``*``=` `2` `    ``# N is power of 2``    ``if` `(N & (N ``-` `1``) ``=``=` `0``):``        ``return` `N ``/``/` `2``    ``else``:``        ``return` `N ``-` `temp ``+` `1` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``N ``=` `7``    ``print``(maximumSizeOfSet(N))` `# This code is contributed by rakeshsahni`

## C#

 `// C# program for the above approach``using` `System;` `public` `class` `GFG {` `    ``static` `int` `maximumSizeOfSet(``int` `N)``    ``{` `        ``// Base Case``        ``if` `(N == 1)``            ``return` `1;` `        ``// Store the power of 2 less than``        ``// or equal to N``        ``int` `temp = 1;``        ``while` `(temp * 2 <= N) {``            ``temp *= 2;``        ``}` `        ``// N is power of 2``        ``if` `((N & (N - 1)) == 0)``            ``return` `N / 2;``        ``else``            ``return` `N - temp + 1;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `N = 7;` `        ``Console.WriteLine(maximumSizeOfSet(N));``    ``}``}` `// This code is contributed by dwivediyash`

## Javascript

 ``

Output:

`4`

Time Complexity: O(log N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up