Minimum Cost to Connect All Points Problem Statement

Given an array COORDINATES representing the integer coordinates of some points on a 2D plane, determine the minimum cost required to connect all points. The cost to connect two points, (x1, y1) and (x2, y2), is defined by their Manhattan distance: |x1 - x2| + |y1 - y2|.

Input:

The first line of input contains an integer 'T' representing the number of test cases. The first line of each test case contains an integer ‘N’ representing the number of points in the ‘COORDINATES’ array. The next ‘N’ lines of each test case contain two space-separated integers representing the ‘X’ and ‘Y’ coordinates of a point.

Output:

For each test case, output a single integer denoting the minimum cost to connect all points. Each test case's result should be printed on a new line.

Example:

Consider the input:

2
3
0 0
2 2
3 10
4
0 0
10 10
20 20
5 5

Expected output:

12
30

Constraints:

  • 1 ≤ T ≤ 5
  • 1 ≤ N ≤ 1000
  • 10^{-6} ≤ X, Y ≤ 10^{6}
  • All points are distinct.

Note:

You do not need to implement input/output handling. The focus should be on the function logic to compute the minimum cost.

Ensure all points are connected with one simple path only, as defined by path terminology in graph theory.

AnswerBot
4mo

Calculate the minimum cost to connect all points on a 2D plane using Manhattan distance.

  • Iterate through all pairs of points and calculate Manhattan distance between them

  • Use a minimum spanning tree alg...read more

Help your peers!
Select
Add answer anonymously...

TCS System Engineer interview questions & answers

A System Engineer was asked 5d agoQ. Could you explain the concepts of Object-Oriented Programming (OOP)?
A System Engineer was asked 1w agoQ. What is unsupervised and supervised machine learning?
A System Engineer was asked 1w agoQ. What are the various steps of EDA (exploratory data analysis)?

Popular interview questions of System Engineer

A System Engineer was asked 2w agoQ1. What is unsupervised and supervised machine learning?
A System Engineer was asked 2w agoQ2. What are the various steps of EDA (exploratory data analysis)?
A System Engineer was asked 3w agoQ3. What is meant by SQL queries?

Top HR questions asked in TCS System Engineer

A System Engineer was asked 1mo agoQ1. Are you open to the possibility of relocating?
A System Engineer was asked 1mo agoQ2. What can you explain about internships and the differences between mini and majo...read more
A System Engineer was asked 1mo agoQ3. Can you describe the project you completed at the university level?
TCS System Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits