Closest House
Practice
4.5 (4 votes)
C++
Math
Sorting
Basic math
Problem
34% Success 209 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

You are given an array \(A\) of \(N\) points in a \(2-D\) plane, which denotes the location of houses of students. The students decided to meet at one of the houses to study together.

For travelling from a point in the plane \([ X_1, Y_1 ]\) to the point \([ X_2, Y_2]\), a student needs to travel a total distance of \(abs|X_1 - X_2| + abs|Y_1- Y_2|\) i.e. the sum of the absolute difference between the differences in both the \(X\) and the \(Y\) coordinates.

Initially all the students are at their own house and they decide to meet in the house whose location is present in the array \(A\) such that the sum of total distances covered by other students to reach this house is minimum.

Input Format:

  • The first line contains an integer \(T\), which denotes the number of test cases.
  • The first line of each test case contains an integer \(N\), denoting the number of points in the plane.
  • The next \(N\) lines of each test case contains \(2\) space-separated integers, \(X\) and \(Y\), the coordinates of the houses on the plane.

Output Format:

For each test case, print the minimum distance travelled by all the students provided they choose the most optimal house.

Constraints:

\(1 <= T <= 10\)

\(1 <= N <= 10^5\)

\(1 <= X, Y <= 10^3\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
10 votes
Tags:
MathPrefixBasic MathBasic ProgrammingDynamic Programming
Points:30
10 votes
Tags:
MediumBasic MathMathematicsMathematics
Points:30
Tags:
Basic ProgrammingMediumMath