Given a list of ordered pairs, and the order they should be placed in a line, find the sum of the absolute values of the differences.
list = [1 2
5 3
2 4
order = [1 3 2]
yields: [1 2][2 4][5 3]
or: abs(2-2) + abs(4-5)
or: 0 + 1
or: 1
Solution Stats
Problem Comments
3 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers903
Suggested Problems
-
1063 Solvers
-
Project Euler: Problem 3, Largest prime factor
1794 Solvers
-
Getting the row and column location from a matrix
299 Solvers
-
795 Solvers
-
Find the sides of an isosceles triangle when given its area and height from its base to apex
2157 Solvers
More from this Author51
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Is Test #3 correct? When I solve it with pencil and paper, I get 15 instead of 14.
[5 4][1 2][2 4][7 5][4 8][4 5][1 6]
= -3 + 0 + 3 + -1 + -4 + -4
= 3+0+3+1+4+4
=15
Have I missed something?
No it's 14. With the ordering, you should get:
[5 4][1 2][2 4][4 8][1 6][4 5][7 5], and the sum is abs(4-1) + abs(2-2) + abs(4-4) + abs(8-1) + abs(6-4) + abs(5-7) = 3 + 0 + 0 + 7 + 2 + 2 = 14
good