Implement Euclidean Distance
Implement Euclidean Distance
The Euclidean distance (L2 distance) between two equal-length vectors x, y is:
d(x,y)=i∑(xi−yi)2Examples
Input: x = [3,4], y = [0,0]
Output: 5.0
√((3-0)² + (4-0)²) = √(9 + 16) = √25 = 5
Input: x = [1,2,3], y = [4,5,6]
Output: 5.196152422706632
√((1-4)² + (2-5)² + (3-6)²) = √(9 + 9 + 9) = √27 ≈ 5.196
Input: x = [0,0,0], y = [0,0,0]
Output: 0.0
Hint 1
Convert inputs to NumPy arrays and then compute
Requirements
- Must work for lists or NumPy arrays
- Must return a float
- Must be vectorized (no Python element loops)
Constraints
- Time limit: 200 ms, Memory: 64 MB
- NumPy only (no sklearn, scipy)
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array
Implement Euclidean Distance
Implement Euclidean Distance
The Euclidean distance (L2 distance) between two equal-length vectors x, y is:
d(x,y)=i∑(xi−yi)2Examples
Input: x = [3,4], y = [0,0]
Output: 5.0
√((3-0)² + (4-0)²) = √(9 + 16) = √25 = 5
Input: x = [1,2,3], y = [4,5,6]
Output: 5.196152422706632
√((1-4)² + (2-5)² + (3-6)²) = √(9 + 9 + 9) = √27 ≈ 5.196
Input: x = [0,0,0], y = [0,0,0]
Output: 0.0
Hint 1
Convert inputs to NumPy arrays and then compute
Requirements
- Must work for lists or NumPy arrays
- Must return a float
- Must be vectorized (no Python element loops)
Constraints
- Time limit: 200 ms, Memory: 64 MB
- NumPy only (no sklearn, scipy)
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array