Show problems as
Classic problem #367EasyMathBinary Search

Valid Perfect Square

Real-world version: Square Crate Block
Tracked in this browser only
Read Valid Perfect Square on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstBasic Binary Search · Binary Search, animated step by step

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.

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