import scala.collection.mutable.PriorityQueue object KClosestPointsToOrigin { def kClosest(points: Array[Array[Int]], k: Int): Array[Array[Int]] = { // Define a custom ordering for points based on their distance to the origin implicit val pointOrdering: Ordering[Array[Int]] = Ordering.by(point => point(0) * point(0) + point(1) * point(1)) // Create a min-heap (priority queue) to store the K closest points val minHeap = PriorityQueue.empty[Array[Int]] // Iterate through each point and maintain the K closest points in the min-heap for (point <- points) { minHeap.enqueue(point) if (minHeap.size > k) { // If the min-heap size exceeds K, remove the farthest point minHeap.dequeue() } } // Convert the min-heap to an array and return the result minHeap.toArray } def main(args: Array[String]): Unit = { val points = Array(Array(1, 3), Array(-2, 2), Array(2, -2), Array(0, 1)) val k = 2 val closestPoints = kClosest(points, k) // Print the K closest pointsimport scala.collection.mutable.PriorityQueue object KClosestPointsToOrigin { def kClosest(points: Array[Array[Int]], k: Int): Array[Array[Int]] = { // Define a custom ordering for points based on their distance to the origin implicit val pointOrdering: Ordering[Array[Int]] = Ordering.by(point => point(0) * point(0) + point(1) * point(1)) // Create a min-heap (priority queue) to store the K closest points val minHeap = PriorityQueue.empty[Array[Int]] // Iterate through each point and maintain the K closest points in the min-heap for (point <- points) { minHeap.enqueue(point) if (minHeap.size > k) { // If the min-heap size exceeds K, remove the farthest point minHeap.dequeue() } } // Convert the min-heap to an array and return the result minHeap.toArray } def main(args: Array[String]): Unit = { val points = Array(Array(1, 3), Array(-2, 2), Array(2, -2), Array(0, 1)) val k = 2 val closestPoints = kClosest(points, k) // Print the K closest pointsimport scala.collection.mutable.PriorityQueue object KClosestPointsToOrigin { def kClosest(points: Array[Array[Int]], k: Int): Array[Array[Int]] = { // Define a custom ordering for points based on their distance to the origin implicit val pointOrdering: Ordering[Array[Int]] = Ordering.by(point => point(0) * point(0) + point(1) * point(1)) // Create a min-heap (priority queue) to store the K closest points val minHeap = PriorityQueue.empty[Array[Int]] // Iterate through each point and maintain the K closest points in the min-heap for (point <- points) { minHeap.enqueue(point) if (minHeap.size > k) { // If the min-heap size exceeds K, remove the farthest point minHeap.dequeue() } } // Convert the min-heap to an array and return the result minHeap.toArray } def main(args: Array[String]): Unit = { val points = Array(Array(1, 3), Array(-2, 2), Array(2, -2), Array(0, 1)) val k = 2 val closestPoints = kClosest(points, k) // Print the K closest points