Given a positive integer s, let A be a 3D array of dimensions n × n × n, where each element A[i][j][k] is defined as:
A[i][j][k] = i * (j OR k), where 0 <= i, j, k < n.Return the maximum possible value of n such that the sum of all elements in array A does not exceed s.
Example 1:
Input: s = 10
Output: 2
Explanation:
A for n = 2:
A[0][0][0] = 0 * (0 OR 0) = 0A[0][0][1] = 0 * (0 OR 1) = 0A[0][1][0] = 0 * (1 OR 0) = 0A[0][1][1] = 0 * (1 OR 1) = 0A[1][0][0] = 1 * (0 OR 0) = 0A[1][0][1] = 1 * (0 OR 1) = 1A[1][1][0] = 1 * (1 OR 0) = 1A[1][1][1] = 1 * (1 OR 1) = 1A is 3, which does not exceed 10, so the maximum possible value of n is 2.Example 2:
Input: s = 0
Output: 1
Explanation:
A for n = 1:
A[0][0][0] = 0 * (0 OR 0) = 0A is 0, which does not exceed 0, so the maximum possible value of n is 1.
Constraints:
0 <= s <= 1015Problem Overview: You need to determine the maximum possible array size n such that the total cost defined by specific bit positions across numbers 1..n does not exceed a given limit. The challenge is computing this cost efficiently without iterating through every number.
Approach 1: Direct Simulation (Brute Force) (Time: O(n log n), Space: O(1))
The most straightforward idea is to iterate through every number from 1 to n, examine its binary representation, and count the bits that contribute to the cost based on the rule (for example, specific bit positions). Accumulate this cost until the limit is exceeded. While this approach is simple to reason about, it becomes infeasible for large n because each number requires inspecting its bits. Even with efficient bit operations, the total work grows linearly with n.
Approach 2: Preprocessing + Binary Search (Time: O(log n * log n), Space: O(1))
The key observation is that the cost function is monotonic: as n increases, the total cost for the range 1..n never decreases. This property allows you to apply binary search on the answer. Instead of constructing the array, you guess a candidate size mid and compute the total cost contributed by all numbers from 1 to mid. If the cost stays within the allowed limit, the size is feasible and you search to the right; otherwise you search to the left.
The remaining problem is computing the contribution of each bit position efficiently. Using patterns in binary representations, you can count how many numbers in 1..mid have a specific bit set. For a bit position b, the pattern repeats every 2^(b+1) numbers. Half of each cycle contributes a set bit. This lets you compute counts in O(1) per bit instead of scanning every number. Only the relevant positions (for example those divisible by a given constraint) need to be considered. This technique relies heavily on bit manipulation and simple arithmetic patterns.
Combining these ideas gives a fast feasibility check inside binary search. Each check iterates through possible bit positions (roughly log n of them) and accumulates their contributions. The binary search itself also runs for log n steps, making the overall complexity O(log n * log n), which easily handles very large ranges.
Recommended for interviews: The preprocessing + binary search approach is the expected solution. Brute force demonstrates understanding of the cost definition, but recognizing the monotonic property and applying binary search with bit-counting formulas shows strong algorithmic thinking and familiarity with bit manipulation patterns.
We can roughly estimate the maximum value of n. For j \lor k, the sum of the results is approximately n^2 (n - 1) / 2. Multiplying this by each i \in [0, n), the result is approximately (n-1)^5 / 4. To ensure (n - 1)^5 / 4 leq s, we have n leq 1320.
Therefore, we can preprocess f[n] = sum_{i=0}^{n-1} sum_{j=0}^{i} (i \lor j), and then use binary search to find the largest n such that f[n-1] cdot (n-1) cdot n / 2 leq s.
In terms of time complexity, the preprocessing has a time complexity of O(n^2), and the binary search has a time complexity of O(log n). Therefore, the total time complexity is O(n^2 + log n). The space complexity is O(n).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Simulation (Brute Force) | O(n log n) | O(1) | Useful for understanding the cost definition or verifying small test cases |
| Preprocessing + Binary Search | O(log n * log n) | O(1) | Optimal for large constraints where the answer range is huge and the cost function is monotonic |
LeetCode was HARD until I Learned these 15 Patterns • Ashish Pratap Singh • 1,002,307 views views
Watch 9 more video solutions →Practice Maximum Sized Array with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor