Can Make Arithmetic Progression From Sequence
Real-world version: Even-Spaced Survey MarksThe statement here is our own: the same inputs, outputs and technique.
A survey crew has a bag of elevation marks, marks, each one a whole number of centimetres relative to the site datum, so a mark below the datum is negative. The marks come out of the bag in no particular order.
The crew wants to know whether the marks can be laid out in some order so that the gap between each mark and the next one is the same all the way along. The shared gap may be positive, negative or zero.
Return true if such an order exists and false otherwise.
Examples
Example 1
- Input
- marks = [12, 4, 8, 0]
- Output
- true
Laid out as 0, 4, 8, 12 the gap is 4 every time.
Example 2
- Input
- marks = [-6, -1, 4, 8]
- Output
- false
There is no order of -6, -1, 4 and 8 whose gaps are all the same.
Example 3
- Input
- marks = [-10, 5, -25, 20]
- Output
- true
Laid out as -25, -10, 5, 20 the gap is 15 every time.
Example 4
- Input
- marks = [7, 7, 7]
- Output
- true
Three identical marks sit at a gap of 0 from each other.
Example 5
- Input
- marks = [3, 3, 6]
- Output
- false
No order of 3, 3 and 6 has a single shared gap.
Constraints
- 2 <= marks.length <= 1000
- -10^6 <= marks[i] <= 10^6
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 can_space_evenly(marks: list[int]) -> bool:public boolean canSpaceEvenly(int[] marks)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 passed
- Passed: 5 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