Ctrl K

OR Cube Within a Budget

Medium
For a positive integer n, build an n × n × n cube. The cell at (x, y, z), where 0 ≤ x, y, z < n, holds the number x * (y | z), with | meaning bitwise OR. The cost of a cube is the sum of the numbers in all its cells. Given an integer budget, return the largest side n for which the cost of the cube does not exceed budget. A cube of side 1 always costs 0, so the answer is at least 1.
Example 1
budget=3 → 2

For n = 2 only the cells with x = 1 are non-zero: 1·(0|1) + 1·(1|0) + 1·(1|1) = 3, exactly the budget. For n = 3 the total is already 45.

Example 2
budget=44 → 2

A cube of side 3 costs 45, one more than the budget, so the answer is 2.

Example 3
budget=0 → 1

A cube of side 1 is a single cell (0, 0, 0) with value 0, so it fits any budget.

Constraints
  • 0 ≤ budget ≤ 10¹⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSpot the Extra Copy MediumShortest Full-OR Stretch From Each Start MediumBit Tax Within Budget MediumSize of a Packed Tree EasyThe Lost Ticket Easy
loading Python…
Input
budget=3
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 65 tests on the server, hidden ones included.