Show problems as
Classic problem #1502EasyArraySorting

Can Make Arithmetic Progression From Sequence

Real-world version: Even-Spaced Survey Marks
Tracked in this browser only
Read Can Make Arithmetic Progression From Sequence on LeetCode(opens in a new tab)

The 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.

Python
def can_space_evenly(marks: list[int]) -> bool:
Java
public boolean canSpaceEvenly(int[] marks)