81. Minimum Distance Between Points

Imagine you're building a mapping or sensor system that tracks positions along a straight line (like the X-axis). For optimization or collision detection, you need to find the closest two data points in terms of distance. This is a common operation in spatial analysis and robotics path planning.

You are given a table Point:

╔═════════════╦══════════╗
ā•‘ Column Name ā•‘   Type   ā•‘
╠═════════════╬══════════╣
ā•‘      x      ā•‘   int    ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • x: X-coordinate of a point on the X-axis. It is the primary key, so all values are unique.

Write a query to find the shortest distance between any two points listed in the Point table.

Example 1:

Example:

Input:

Point Table

╔═════╗
ā•‘  x  ā•‘
╠═════╣
ā•‘ -1  ā•‘
║─────║
ā•‘  0  ā•‘
║─────║
ā•‘  2  ā•‘
ā•šā•ā•ā•ā•ā•ā•

Expected Output:

╔═══════════╗
ā•‘ shortest  ā•‘
╠═══════════╣
ā•‘     1     ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Explanation:

  • Distances: |(-1) - 0| = 1, |(-1) - 2| = 3, |0 - 2| = 2
  • The minimum distance is 1.

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Hints

0
Ā 
Test Case

Input:

Point