Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6629567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:16:44+00:00 2026-05-25T22:16:44+00:00

This question was asked to me in an interview and it embarrassingly exposed my

  • 0

This question was asked to me in an interview and it embarrassingly exposed my shortcomings on dynamic programming. I will appreciate if someone can help me crack this one. Also, it would be very helpful to me (and others) if you can explain your thinking process along the way as you devise the solution as i seem to be able to understand when i see a solution which uses dynamic programming paradigm but struggle to come up with my own.

Without further ado, here is the question i was asked.

Given an integer i and set X of k points x1, x2, … xk on real line, select i points from set X so as to minimize the sum of the distance from every point in X to a point in i using Dynamic programming.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:16:45+00:00Added an answer on May 25, 2026 at 10:16 pm

    With most DP problems I try and find a kind of reduce-and-conquer relation. That is, a relation whereby I can cut away from the problem size with each step (like divide and conquer, but usually doesn’t divide the problem, it just removes a small part). In this problem (like many others) we can make a very simple observation: Either the first point is in the set of i points, or it isn’t.

    Some notation: Let’s say X = {x1, x2, …, xk}, and denote the reduced set Xn = {xn, xn+1, …, xk}.

    So the observation is that either x1 is one of the i points, or it isn’t. Let’s call our i-set finding function MSD(i,Xk) (minimum sum of distances). We can express that cut-away observation as follows:

    MSD(i,Xk) = Either MSD(i-1,Xk-1) U {x1} or MSD(i,Xk-1)

    We can formalise the “either or” part by realising a simple way of checking which of those two options it actually is: We run through the set X and calculate the sum of the distances, and check which is actually the smaller. We note at this point, that that check has a running time of ki since we will naively run through each of the k points and grab the minimum distance from points in the set of size i.

    We make two simple observations regarding base cases:

    MSD(i,Xi) = Xi
    MSD(0,Xn) = {}

    The first is that when looking for i points in a set of size i we obviously just take the whole set.
    The second is that when looking for no points in a set, we return the empty set. This inductively ensures that MSD returns sets of size i (it’s true for the case where i=0 and by induction is true according to our definition of MSD above).

    That’s it. That will find the appropriate set.
    Runtime complexity is upper bounded by O(ik * step) where step is our O(ik) check from above. This is because MSD will be run on parameters that range from 0-i and X1 – Xk, which is a total of ik possible arguments.

    That leaves us with a runtime of O((ik)2).

    The following part is based on my understanding of the OP’s question. I’m not sure if the distance of every point in X from the i-sized subset is the sum of the distances of every point from every other point in the subset, or the sum of the distances of every point in X from the subset itself.
    I.e. sigma of x in X of (sum of distances of x from every point in the subset) OR sigma of x in X of (distance of x from the subset which is the minimum distance from x to any point in the subset)

    I assume the latter.

    We can reduce the runtime by optimising the O(ik) check from above. We notice that the elements are actually sorted (albeit in reverse order in this current notation), since when we add them on we do so always from the right hand side. Assuming they’re sorted to begin with, they will be once out of the MSD routine. If they weren’t sorted to begin with we can sort them, which will only cost O(klogk) anyway.

    Once sorted, checking the distance of each point from a point in the set will be k * logi since for each point we do a binary search. This yields a total running time of O(ik * klogi + klogk)
    = O(k2 * ilogi).

    Finally, we can express that as O(k3logk). Not the fastest solution, but a solution.

    I’m sure there are even more optimisations, but that’s my 2c.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This was the question asked in interview. Can we call one constructor from another
I was asked this question in an interview would appreciate your answers. Regards; Mohammad
This question was asked of me in an interview. How can we convert a
This question was asked at interview. Say I have a contract. [ServiceContract] public interface
This question was asked to me in an interview. Suppose char *p=malloc(n) assigns more
This is an interview question asked a month ago.... Do session use cookies? If
I was asked this question during an interview. They're both O(nlogn) and yet most
I was asked this question in a job interview. The interviewer and I disagreed
I was asked this question in a job interview, and I'd like to know
Yesterday in my interview I was asked this question. (At that time I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.