Maximum Sized Array - Problem
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 and OR is the bitwise OR operation.
Return the maximum possible value of n such that the sum of all elements in array A does not exceed s.
Input & Output
Example 1 — Small Budget
$
Input:
s = 6
›
Output:
2
💡 Note:
For n=2: sum = 0*(0|0) + 0*(0|1) + 0*(1|0) + 0*(1|1) + 1*(0|0) + 1*(0|1) + 1*(1|0) + 1*(1|1) = 0+0+0+0+0+1+1+1 = 3. For n=3: sum would be much larger. So maximum n=2.
Example 2 — Larger Budget
$
Input:
s = 100
›
Output:
4
💡 Note:
For n=4, the sum of all A[i][j][k] = i*(j|k) values is within budget 100, but n=5 would exceed it.
Example 3 — Minimum Case
$
Input:
s = 0
›
Output:
1
💡 Note:
For n=1: only A[0][0][0] = 0*(0|0) = 0, so sum=0 ≤ 0. For n=2, sum=3 > 0. Maximum n=1.
Constraints
- 1 ≤ s ≤ 109
- The answer n will be at most 104
Visualization
Tap to expand
Understanding the Visualization
1
3D Array
Each A[i][j][k] = i * (j OR k)
2
Sum Constraint
Find max n where total sum ≤ s
3
Binary Search
Efficiently find the answer
Key Takeaway
🎯 Key Insight: The sum is monotonically increasing with n, enabling binary search on the answer space for optimal efficiency
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code