K Centers Selection Problem

In Ninja Land, there are cities numbered from 0 to N-1. The distances between each pair of cities are represented by an N * N matrix 'DIST', where 'DIST[i][j]' is the distance between city 'i' and city 'j'.

Ninja plans to install Coding Ninjas servers in K cities and needs to ensure that the maximum distance from any city to a nearest server is as small as possible.

Input:

The first line contains an integer T, the number of test cases.
Each test case starts with two integers N and K, where N is the number of cities, and K is the number of server installations.
The next N lines contain N integers each, representing the matrix 'DIST'.

Output:

For each test case, output a single integer representing the optimized maximum distance of any city from the nearest server.

Example:

Input:
T = 1
N = 4, K = 2
DIST =
[0, 10, 7, 6]
[10, 0, 8, 5]
[7, 8, 0, 12]
[6, 5, 12, 0]
Output:
6
Explanation:

Choose cities 2 and 3 for the server installation. The maximum minimized distance is 6.

Constraints:

  • 1 ≤ T ≤ 50
  • 1 ≤ N ≤ 15
  • 1 ≤ K ≤ N
  • 1 ≤ DIST[i][j] ≤ 109, for i ≠ j
  • DIST[i][j] = 0, for i = j

Note:

It is not necessary to print the output; just implement the function as required.
AnswerBot
4mo

The problem involves selecting K cities to install servers in Ninja Land to minimize the maximum distance from any city to a nearest server.

  • Iterate through all possible combinations of K cities to sel...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Jaguar Land Rover

Q. SELL ABD BUY 1 array question(easy level)
Q. Beautiful String Verification Given a non-empty string inputString, determine if...read more
Q. Complex Number Multiplication Calculate the product of two complex numbers repre...read more
Software Developer 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