Shortest Distance in a Line

easy database sql

Problem

Point(x). Return the shortest distance between any two distinct points on the x-axis.

Inputp=[(-1,),(0,),(2,)]
Output1
Sorted: -1, 0, 2. Shortest gap is 0-(-1)=1.

SELECT MIN(p2.x - p1.x) AS shortest
FROM Point p1 JOIN Point p2 ON p1.x < p2.x;
Time: O(n) Space: O(n)