Lazy Santa

It’s christmas and Santa is here with ‘K’ gifts. There are ‘N’ children in the park who are standing in a straight line, and not crowding up 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 different distance from the gate.

Since Santa is still on his reindeer, he can land at any position in the park. But once he lands on the ground, he uses his feet to walk. Santa is very lazy and is requesting you to find the minimum distance he needs to travel on his feet so that he can distribute all the ‘K’ gifts to ‘K’ different children.

Example:
Let there be 6 children and 3 gifts. If the position of the children is [3, 6, 7, 10, 17, 25], the minimum distance Santa has to walk is 4m which can be achieved in many ways, one of them being Santa landing at the 1st child and walking till the 3rd child (distance = (6m - 3m) + (7m - 6m) = 4m).
Input Format:
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.

The first line of each test case contains two space-separated integers, ‘N’ and ‘K’ denoting the number of children and number of gifts respectively.

The next line contains ‘N’ space-separated integers denoting the array “distance”.
Output Format :
For each test case, return an integer denoting the minimum distance Santa has to travel.

Output for each test case will be printed in a new line. 
Note:
You do not need to print anything; it has already been taken care of. Just implement the given functions.
Constraints:
1 <= T <= 100
1 <= N <= 10000
1 <= K <= 10000
1 <= distance[i] < 10^9


Time Limit: 1 sec
CodingNinjas
author
2y
Brute Force

We can note that it is always optimal to distribute gifts to ‘K’ children who are adjacent to each other.

For example, if the position of children are [2, 4, 7] and K = 2, it is optimal to...read more

Help your peers!
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
Get AmbitionBox app

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