← Questions

Largest square from 1x1 and 2x2 tiles

0
MicrosoftCoding

You have M small tiles of size 1×1 and N large tiles of size 2×2. Build the largest possible square: every cell of the square must be covered by exactly one tile, with no gaps, no overlaps, and no tile sticking out past the edge. You do not have to use all of your tiles.

Implement:

def max_square_side(m: int, n: int) -> int: ...

Return the side length of the largest square you can build, or 0 if you cannot build any square.

Constraints

  • 0 <= M, N <= 1_000_000_000
  • Aim for better than linear time in M + N.

Example 1

M = 5, N = 1 returns 3. Put the 2×2 tile in one corner of a 3×3 square and fill the remaining 5 cells with 1×1 tiles.

Example 2

M = 0, N = 3 returns 2. The tiles have total area 12, but three 2×2 tiles cannot fill a 3×3 square (area 9 is not a multiple of 4) and a 4×4 square needs four of them. One 2×2 tile alone makes a 2×2 square.

Example 3

M = 0, N = 0 returns 0.

Reconstructed from the reported question; details may differ from what interviewers ask.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding