Problems
Loading...
1 / 1

Implement Manhattan Distance

Linear Algebra
Easy

The Manhattan distance (L1 distance) between two equal-length vectors x, y is:

d(x,y)=ixiyid(x, y) = \sum_{i} |x_i - y_i|
Loading visualization...

Examples

Input: x = [1,2,3], y = [2,4,6]

Output: 6.0

|1-2| + |2-4| + |3-6| = 1 + 2 + 3 = 6

Input: x = [-1,-2], y = [1,2]

Output: 6.0

|-1-1| + |-2-2| = 2 + 4 = 6

Input: x = [0,0,0], y = [0,0,0]

Output: 0.0

Hint 1

Convert inputs to NumPy arrays first, then use vectorized operations to compute absolute differences.

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