Skip to main content

Maximum Sized Array - Solution & Explanation

MediumPremiumFree on FleetCodeBinary SearchBit Manipulation8 min read
Practice this problem

Problem Statement

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:

  • Elements of the array A for n = 2:
    • A[0][0][0] = 0 * (0 OR 0) = 0
    • A[0][0][1] = 0 * (0 OR 1) = 0
    • A[0][1][0] = 0 * (1 OR 0) = 0
    • A[0][1][1] = 0 * (1 OR 1) = 0
    • A[1][0][0] = 1 * (0 OR 0) = 0
    • A[1][0][1] = 1 * (0 OR 1) = 1
    • A[1][1][0] = 1 * (1 OR 0) = 1
    • A[1][1][1] = 1 * (1 OR 1) = 1
  • The total sum of the elements in array A is 3, which does not exceed 10, so the maximum possible value of n is 2.

Example 2:

Input: s = 0

Output: 1

Explanation:

  • Elements of the array A for n = 1:
    • A[0][0][0] = 0 * (0 OR 0) = 0
  • The total sum of the elements in array A is 0, which does not exceed 0, so the maximum possible value of n is 1.

 

Constraints:

  • 0 <= s <= 1015

Approach Overview

Problem 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.

Solution

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).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen 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 SearchO(log n * log n)O(1)Optimal for large constraints where the answer range is huge and the cost function is monotonic

Video Solution

LeetCode was HARD until I Learned these 15 Patterns • Ashish Pratap Singh • 1,002,307 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Maximum Sized Array easy or hard?
Maximum Sized Array is generally considered a Medium difficulty problem. The main challenge is recognizing that the cost function is monotonic and that bit contributions can be counted mathematically instead of iterating through every number.
Maximum Sized Array Python/Java solution
The implementation is identical across languages: binary search the answer and compute the cost with bit operations. Python, Java, C++, Go, and TypeScript versions all follow the same logic with loops over bit positions and integer arithmetic.
What is the best approach for Maximum Sized Array?
The optimal solution uses binary search on the answer combined with bit-counting formulas. Because the total cost for numbers 1..n increases monotonically, you can binary search the largest valid n and compute the cost using bit manipulation patterns. This reduces the complexity to O(log n * log n).
Is Maximum Sized Array asked at Google/Amazon/Meta?
Problems combining binary search on the answer with bit manipulation frequently appear in interviews at large tech companies. Variants of bit-counting and range bit contribution problems are common in Google and Meta interview question sets.
What data structure is used in Maximum Sized Array?
The solution does not rely on complex data structures. It mainly uses arithmetic, bit manipulation, and binary search over the answer space. The key technique is counting set bits at specific positions using repeating binary cycles.
What is the time complexity of Maximum Sized Array?
The optimized approach runs in O(log n * log n) time. Binary search contributes one log n factor, and each feasibility check iterates over possible bit positions (also about log n). Space complexity is O(1).
How to solve Maximum Sized Array in O(log n * log n)?
Binary search the largest possible array size n. For each candidate value, compute the total cost contributed by relevant bit positions across numbers 1..n using repeating binary patterns. Counting set bits per position avoids iterating through every number.

Ready to solve this problem?

Practice Maximum Sized Array with our built-in code editor and test cases.

Practice on FleetCode