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
AnswerBot
4mo

Find the minimum distance Santa must travel to distribute 'K' gifts to 'K' different children standing in a straight line.

  • Sort the array 'distance' to easily calculate the minimum distance Santa has t...read more

Help your peers!
Select
Add answer anonymously...

Apple Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Kevin and his Fruits Problem Statement Kevin has 'N' buckets, each consisting of...read more
A Software Developer Intern was asked Q. Max GCD Pair Problem Statement Given an array of positive integers, determine th...read more
A Software Developer Intern was asked Q. Lazy Santa Problem Statement It's Christmas and Santa has 'K' gifts to distribut...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Kevin and his Fruits Problem Statement Kevin has 'N' buckets, each consisting of...read more
A Software Developer Intern was asked Q2. Max GCD Pair Problem Statement Given an array of positive integers, determine th...read more
A Software Developer Intern was asked Q3. Lazy Santa Problem Statement It's Christmas and Santa has 'K' gifts to distribut...read more
Apple Software Developer Intern 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