All problems
0300EasyMathBinary Search

Square Crate Block

Tracked in this browser only
Write code

Trains the technique from

LeetCode 367Valid Perfect Square

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A depot has crates identical crates and wants to set them out as one solid square block on the floor: the same number of rows as columns, every position filled, and not a single crate left over or borrowed.

Given crates, return true when a whole number of rows exists that makes such a block, and false when it does not.

Settle it with arithmetic on integers. Do not reach for a square-root or power routine from the standard library, and do not float the count through a fractional type on the way.

Examples

Example 1

Input
crates = 808201
Output
true

899 rows of 899 crates each use exactly 808201 crates.

Example 2

Input
crates = 11
Output
false

A 3 by 3 block uses 9 and leaves 2 spare, and a 4 by 4 block would need 5 crates more, so no square block fits 11 exactly.

Constraints

  • 1 <= crates <= 2^31 - 1

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def fits_square_block(crates: int) -> bool:
Java
public boolean fitsSquareBlock(int crates)
September 7
Apply