-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20230124.js
50 lines (38 loc) · 1.1 KB
/
20230124.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Lang: JavaScript
Type: shortest
Problem:
**Introduction**:
The "Manhattan Distance" is the distance between two points in an N dimensional space. In simple terms, it is the sum of absolute differences between the measures in all dimensions of two points.
2D Example:
Point1: (x1, y1) Point2: (x2, y2) Manhattan Distance: |x1-x2| + |y1-y2|;
3D Example:
Point1: (x1, y1, z1) Point2: (x2, y2, z2) Manhattan Distance: |x1-x2| + |y1-y2| + |z1-z2|
**Challenge**:
Given two points A and B, calculate the Manhattan Distance between them.
Input
Line 1 : N an integer, the number of dimensions.
Line 2 : N integers, the coordinate of point A.
Line 3 : N integers, the coordinate of point B.
Output
An integer answer, the manhattan distance between A and B.
Constraints
1 ≤ N ≤ 10
-1000 ≤ coordinates ≤ 1000
Example
Input
2
0 0
2 2
Output
4
Solution:
const N = parseInt(readline());
var inputs = readline().split(' ');
var inputs2 = readline().split(' ');
var sum = 0
for (let i = 0; i < N; i++) {
const ai = parseInt(inputs[i]);
const bi = parseInt(inputs2[i]);
sum += (Math.abs(ai-bi))
}
console.log(sum);