Valid Square
Real-world version: Four Pegs Marking a Square PlotThe statement here is our own: the same inputs, outputs and technique.
A land surveyor has hammered four pegs into a field and recorded each one as a coordinate pair [x, y] on the site grid. The pegs are handed to you as p1, p2, p3 and p4 in no particular order: the recording sequence says nothing about how the pegs sit relative to one another, so the corners of the plot may appear in any of the four positions.
Decide whether the four pegs are exactly the four corners of a square whose side length is greater than zero. All four sides must have the same length and all four corners must be right angles. Pegs that coincide, or that all lie on one straight line, cannot mark such a square.
Return true when the pegs mark a square plot and false otherwise.
Examples
Example 1
- Input
- p1 = [6, 1], p2 = [1, 6], p3 = [1, 1], p4 = [6, 6]
- Output
- true
Walking the pegs as `[1, 1]`, `[6, 1]`, `[6, 6]`, `[1, 6]` gives four sides of length 5 that meet at four right angles, so the pegs mark a square of side 5.
Example 2
- Input
- p1 = [2, 3], p2 = [2, 8], p3 = [2, 13], p4 = [9, 8]
- Output
- false
Three of the pegs sit on the vertical line `x = 2`, so no arrangement of the four gives a shape with four right angles.
Example 3
- Input
- p1 = [3, 0], p2 = [0, 3], p3 = [-3, 0], p4 = [0, -3]
- Output
- true
Walking the pegs as `[3, 0]`, `[0, 3]`, `[-3, 0]`, `[0, -3]` gives four sides that each span 3 across and 3 up or down, and each corner turns through a right angle. A square does not have to be lined up with the grid axes.
Constraints
- p1.length == 2
- p2.length == 2
- p3.length == 2
- p4.length == 2
- -10^4 <= p1[i], p2[i], p3[i], p4[i] <= 10^4
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 is_square_plot(p1: list[int], p2: list[int], p3: list[int], p4: list[int]) -> bool:public boolean isSquarePlot(int[] p1, int[] p2, int[] p3, int[] p4)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 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
More on the same topics
- Rectangle OverlapReal-world version: Do the Two Plots Share Any GroundEasy
- Largest Triangle AreaReal-world version: The Largest Triangle Between PegsEasy
- K Closest Points to OriginReal-world version: Nearest Survey StakesMedium
- Detonate the Maximum BombsReal-world version: Largest Chain of Quarry ChargesMedium