Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-230

Closest Pair Distance

Problem

Given points as [x, y] pairs, return the smallest squared Euclidean distance between any two points, or -1 when fewer than two points are given.

Comparing squared distances avoids floating point and keeps the answer an integer. A brute-force double loop is fine for these input sizes.

Hint

Initialise the best with None or infinity so any first pair becomes the candidate.

Test cases

case 1
in: [[[0,0],[3,4],[1,1]]]
expected: 2
case 2
in: [[[1,1]]]
expected: -1
case 3
in: [[]]
expected: -1
case 4
in: [[[0,0],[0,5]]]
expected: 25
case 5
in: [[[0,0],[1,0],[2,0]]]
expected: 1
Starter codePython
def closest_pair_distance(points):
    # Your code here
    pass

5 test cases · run your code against them in the editor