## prince.sanjose

- 0of 0 votes

AnswerYour organization is opening a new warehouse and your manager has tasked you with figuring out how to quickly get some of the inventory in the original warehouse to the new warehouse. You are given a list of the locations of all N packing crates in the facility, any of which is a candidate to be moved to the new facility. All crates are the same size, and the truck has capacity to move exactly M of them. The delivery driver needs to leave promptly, so it is your job to find the crates that are closest to the truck. Crates are heavy, so you can only carry one at a time.

- prince.sanjose in United States

Given an array of the locations of N packing crates, implement an algorithm to find the locations of the M crates closest to the truck.

Input

The input to the function/method consists of three arguments:

totalCrates, an integer representing the total number of packing crates in the facility (N)allLocations, a list where each element consists of a pair of integers representing the x and y coordinates of the packing crates;

truckCapacity, an integer representing the number of packing crates that will be moved to the new facility (M).

Output

Return a list of integers where each element of the list represents the x and y coordinates of the packing crates that will be moved to the new facility.

Constraints

truckCapacity

totalCrates

Note You begin at the truck’s location [0, 0].The distance of the truck from a warehouse location (x, y) is the square root of x^2 + y^2.If there are ties then return any of the locations as long as you satisfy returning M points.

Example Input:totalCrates = 3allLocations = [[1, 2], [3, 4], [1, -1]]

truckCapacity= 2 Output:[[1, -1], [1, 2]]

Explanation:The distance of the truck from point [1, 2] is square root(5) = 2.236 The distance of the truck from point [3, 4] is square root(25) = 5 The distance of the truck from point [1, -1] is square root(2) = 1.414 num is 2, hence the output is [1, -1] and [1, 2].| Report Duplicate | Flag

Amazon Software Engineer / Developer Java JavaScript

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window