Valid Perfect Square
Real-world version: Square Crate BlockThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def fits_square_block(crates: int) -> bool:public boolean fitsSquareBlock(int crates)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 2 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's