给定一个正整数 K,一个圆心在 (0, 0) 和一些点的坐标。任务是找到圆的最小半径,使至少 k 个点位于圆内。输出最小半径的平方。
例子:
Input : (1, 1), (-1, -1), (1, -1),
k = 3
Output : 2
We need a circle of radius at least 2
to include 3 points.
Input : (1, 1), (0, 1), (1, -1),
k = 2
Output : 1
We need a circle of radius at least 1
to include 2 points. The circle around
(0, 0) of radius 1 would include (1, 1)
and (0, 1).
这个想法是从原点 (0, 0) 找到每个点的欧几里得距离的平方。现在,按递增顺序对这些距离进行排序。现在距离的第 k个元素是所需的最小半径。
下面是这个方法的实现:
C++
// C++ program to find minimum radius
// such that atleast k point lie inside
// the circle
#include
using namespace std;
// Return minumum distance required so that
// aleast k point lie inside the circle.
int minRadius(int k, int x[], int y[], int n)
{
int dis[n];
// Finding distance between of each
// point from origin
for (int i = 0; i < n; i++)
dis[i] = x[i] * x[i] + y[i] * y[i];
// Sorting the distance
sort(dis, dis + n);
return dis[k - 1];
}
// Driven Program
int main()
{
int k = 3;
int x[] = { 1, -1, 1 };
int y[] = { 1, -1, -1 };
int n = sizeof(x)/sizeof(x[0]);
cout << minRadius(k, x, y, n) << endl;
return 0;
}
Java
// Java program to find minimum radius
// such that atleast k point lie inside
// the circle
import java.util.Arrays;
class GFG
{
// Return minumum distance required so that
// aleast k point lie inside the circle.
static int minRadius(int k, int[] x, int[] y,
int n)
{
int[] dis=new int[n];
// Finding distance between of each
// point from origin
for (int i = 0; i < n; i++)
dis[i] = x[i] * x[i] + y[i] * y[i];
// Sorting the distance
Arrays.sort(dis);
return dis[k - 1];
}
// Driven Program
public static void main (String[] args) {
int k = 3;
int[] x = { 1, -1, 1 };
int[] y = { 1, -1, -1 };
int n = x.length;
System.out.println(minRadius(k, x, y, n));
}
}
/* This code is contributed by Mr. Somesh Awasthi */
Python3
# Python3 program to find minimum radius
# such that atleast k point lie inside
# the circle
# Return minumum distance required so
# that aleast k point lie inside the
# circle.
def minRadius(k, x, y, n):
dis = [0] * n
# Finding distance between of each
# point from origin
for i in range(0, n):
dis[i] = x[i] * x[i] + y[i] * y[i]
# Sorting the distance
dis.sort()
return dis[k - 1]
# Driver Program
k = 3
x = [1, -1, 1]
y = [1, -1, -1]
n = len(x)
print(minRadius(k, x, y, n))
# This code is contributed by
# Prasad Kshirsagar
C#
// C# program to find minimum radius
// such that atleast k point lie inside
// the circle
using System;
class GFG {
// Return minumum distance required
// so that aleast k point lie inside
// the circle.
static int minRadius(int k, int []x,
int[] y, int n)
{
int[] dis = new int[n];
// Finding distance between of
// each point from origin
for (int i = 0; i < n; i++)
dis[i] = x[i] * x[i] +
y[i] * y[i];
// Sorting the distance
Array.Sort(dis);
return dis[k - 1];
}
// Driven Program
public static void Main ()
{
int k = 3;
int[] x = { 1, -1, 1 };
int[] y = { 1, -1, -1 };
int n = x.Length;
Console.WriteLine(
minRadius(k, x, y, n));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出:
2
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。