Lazy Santa Problem Statement

It's Christmas and Santa has 'K' gifts to distribute. There are 'N' children standing in a straight line in the park due to COVID restrictions. You are given an array distance containing 'N' integers where distance[i] denotes the distance of the ith child from the gate of the park in meters. Each child stands at a unique distance from the gate.

Santa, being lazy, wants to minimize the distance he needs to walk on foot after landing at any position in the park. Your task is to find the minimum distance Santa must travel to distribute all 'K' gifts to 'K' different children.

Input:

The first line contains an integer ‘T’ representing the number of test cases. Each test case consists of: 
- An integer 'N' and an integer 'K' on the first line, denoting the number of children and the number of gifts respectively.
- A second line with 'N' space-separated integers representing the array distance.

Output:

For each test case, return an integer representing the minimum distance Santa has to walk. Print the output for each test case on a new line.

Example:

Input: 
1
6 3
3 6 7 10 17 25
Output:
4
Explanation:

For the given example, Santa can land at the position of the 1st child and walk to the 3rd child, covering a minimum distance of 4 meters (i.e., (6m - 3m) + (7m - 6m) = 4m).

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 10000
  • 1 <= K <= 10000
  • 1 <= distance[i] < 10^9
  • Time Limit: 1 sec
Be the first one to answer
Add answer anonymously...
SimplifyVMS Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter