All problems
0319EasyArraySorting

Even-Spaced Survey Marks

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1502Can Make Arithmetic Progression From Sequence

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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