Skip to main content

Maximum Number That Makes Result of Bitwise AND Zero - Solution & Explanation

MediumPremiumFree on FleetCodeStringGreedySorting5 min readAsked at: Salesforce
Practice this problem

Problem Statement

Given an integer n, return the maximum integer x such that x <= n, and the bitwise AND of all the numbers in the range [x, n] is 0.

 

Example 1:

Input: n = 7

Output: 3

Explanation:

The bitwise AND of [6, 7] is 6.
The bitwise AND of [5, 6, 7] is 4.
The bitwise AND of [4, 5, 6, 7] is 4.
The bitwise AND of [3, 4, 5, 6, 7] is 0.

Example 2:

Input: n = 9

Output: 7

Explanation:

The bitwise AND of [7, 8, 9] is 0.

Example 3:

Input: n = 17

Output: 15

Explanation:

The bitwise AND of [15, 16, 17] is 0.

 

Constraints:

  • 1 <= n <= 1015

Approach Overview

Problem Overview: You are given an array of integers and need the maximum number of elements you can select such that their combined bitwise AND does not collapse to zero. The key observation is that the AND operation preserves a bit only if every selected number contains that bit.

Approach 1: Brute Force Subset Check (Exponential Time)

Generate all possible subsets and compute the bitwise AND for each subset. Track the largest subset whose result is non-zero. This approach directly models the definition but becomes infeasible quickly because the number of subsets grows as 2^n. Time complexity is O(2^n * n) since each subset may require recomputing the AND across its elements. Space complexity is O(1) excluding recursion or subset storage. Useful only for understanding the behavior of the AND operation on small inputs.

Approach 2: Bit Frequency Counting (Bit Manipulation Greedy) (O(n log A))

The AND of multiple numbers remains non-zero only if there exists at least one bit position where every chosen number has a 1. Instead of checking subsets, iterate through each bit position and count how many numbers contain that bit. If k numbers share the same set bit, their AND will keep that bit, guaranteeing a non-zero result. The maximum valid subset size therefore equals the largest count among all bit positions. Iterate through numbers, check bits using (num >> bit) & 1, and track frequencies. Time complexity is O(n log A) where A is the maximum value (number of bits). Space complexity is O(log A) for bit counters.

This approach relies heavily on bit manipulation and a simple greedy observation: maximizing the group sharing a common bit maximizes the subset that keeps the AND result non-zero. Sorting is unnecessary for the optimal approach, though understanding binary representation patterns is helpful.

Recommended for interviews: The bit counting strategy is what interviewers expect. It reduces an exponential subset search into a linear scan across numbers and bit positions. Mentioning the brute force idea shows understanding of the AND property, but recognizing that a shared bit guarantees a non-zero result demonstrates strong problem decomposition and familiarity with bit-level reasoning.

Solution

We can find the highest bit of 1 in the binary representation of n. The maximum x must be less than n and this bit is 0, and all other lower bits are 1, i.e., x = 2^{number of the highest bit} - 1. This is because x and (x + 1) = 0 must hold.

The time complexity is O(log n), and the space complexity is O(1).

Code

Python

Java

C++

Go

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Subset EnumerationO(2^n * n)O(1)Conceptual understanding or extremely small input sizes
Bit Frequency Counting (Greedy)O(n log A)O(log A)General case and optimal interview solution

Video Solution

3125. Maximum Number That Makes Result of Bitwise AND Zero (Leetcode Medium) • Programming Live with Larry • 162 views views

Frequently Asked Questions

Is Maximum Number That Makes Result of Bitwise AND Zero easy or hard?
The problem is rated Medium because the brute force idea is simple but inefficient. Recognizing that the AND result stays non-zero only when all numbers share a common set bit requires bit manipulation insight.
Maximum Number That Makes Result of Bitwise AND Zero Python/Java solution
The implementation iterates through each number and checks all bit positions using bit shifts. Maintain an array of size 32 (for 32-bit integers) to count set bits. The final answer is the maximum value in this frequency array. This logic works the same in Python, Java, C++, and Go.
How to solve Maximum Number That Makes Result of Bitwise AND Zero in O(n)?
Treat the number of bits in an integer as constant (typically 32). Iterate through the array once and count occurrences of each set bit. The largest frequency among those bits represents the maximum subset whose AND keeps that bit, effectively giving an O(n) practical solution.
What is the best approach for Maximum Number That Makes Result of Bitwise AND Zero?
The optimal approach uses bit manipulation with bit frequency counting. Count how many numbers contain each bit position and take the maximum count. If multiple numbers share a specific set bit, their AND keeps that bit, ensuring the result stays non-zero. This runs in O(n log A) time where A is the maximum value.
Is Maximum Number That Makes Result of Bitwise AND Zero asked at Google/Amazon/Meta?
Bit manipulation and greedy counting problems appear frequently in interviews at companies like Google, Amazon, and Meta. Variants involving maximizing subsets based on shared bits or AND/OR constraints are common in coding interviews.
What data structure is used in Maximum Number That Makes Result of Bitwise AND Zero?
The solution mainly uses simple counters or an array to track how many numbers contain each bit position. No complex data structures are required beyond basic arrays and bitwise operations.
What is the time complexity of Maximum Number That Makes Result of Bitwise AND Zero?
The optimal bit counting solution runs in O(n log A) time because each number is checked across its bit positions. Space complexity is O(log A) for maintaining counts of each bit position.

Ready to solve this problem?

Practice Maximum Number That Makes Result of Bitwise AND Zero with our built-in code editor and test cases.

Practice on FleetCode