Find minimum radius such that atleast k point lie inside the circle
Last Updated :
02 Oct, 2023
Given a positive integer K, a circle center at (0, 0) and coordinates of some points. The task is to find minimum radius of the circle so that at-least k points lie inside the circle. Output the square of the minimum radius.
Examples:
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).
The idea is to find square of Euclidean Distance of each point from origin (0, 0). Now, sort these distance in increasing order. Now the kth element of distance is the required minimum radius.
Below is the implementation of this approach:
C++
// C++ program to find minimum radius
// such that atleast k point lie inside
// the circle
#include<bits/stdc++.h>
using namespace std;
// Return minimum distance required so that
// atleast 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 minimum distance required so that
// atleast 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 minimum distance required so
# that atleast 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 minimum distance required
// so that atleast 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.
JavaScript
<script>
// Javascript program to find minimum radius
// such that atleast k point lie inside
// the circle
// Return minimum distance required so that
// atleast k point lie inside the circle.
function minRadius(k, x, y, n) {
let dis = Array.from({length: n}, (_, i) => 0);
// Finding distance between of each
// point from origin
for (let i = 0; i < n; i++)
dis[i] = x[i] * x[i] + y[i] * y[i];
// Sorting the distance
dis.sort();
return dis[k - 1];
}
// driver function
let k = 3;
let x = [ 1, -1, 1 ];
let y = [ 1, -1, -1 ];
let n = x.length;
document.write(minRadius(k, x, y, n));
// This code is contributed by code_hunt.
</script>
PHP
<?php
// PHP program to find minimum radius
// such that atleast k point lie inside
// the circle
// Return minimum distance required
// so that atleast k point lie
// inside the circle.
function minRadius($k, $x, $y, $n)
{
$dis =array();
// Finding distance between
// of each point from origin
for ($i = 0; $i < $n; $i++)
$dis[$i] = $x[$i] * $x[$i] +
$y[$i] * $y[$i];
// Sorting the distance
sort($dis);
return $dis[$k - 1];
}
// Driver Code
$k = 3;
$x = array(1, -1, 1);
$y = array(1, -1, -1);
$n = count($x);
echo minRadius($k, $x, $y, $n) ;
// This code is contributed by anuj_67.
?>
Time complexity: O(n + nlogn)
Auxiliary Space: O(n)ve.
Approach#2: Using binary search
This code uses binary search to find the minimum radius such that at least k points lie inside or on the circumference of the circle. It first finds the maximum distance between any two points, then performs binary search on the range [0, max_distance] to find the minimum radius.
Algorithm
1. Initialize left = 0 and right = maximum distance between any two points in the given set of points.
2. While left <= right, find mid = (left + right) / 2
3. Check if there exist k points inside or on the circumference of a circle with radius mid using a simple linear search. 4. If k or more points are inside or on the circumference of the circle, set right = mid - 1.
5. If less than k points are inside or on the circumference of the circle, set left = mid + 1.
6. After the binary search, the value of left will be the minimum radius required to include k points.
C++
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
double dist(pair<int, int> p1, pair<int, int> p2)
{
// Calculate Euclidean distance between two points
return sqrt(pow(p1.first - p2.first, 2)
+ pow(p1.second - p2.second, 2));
}
int count_points_in_circle(vector<pair<int, int> > points,
pair<int, int> center,
double radius)
{
// Count the number of points inside or on the
// circumference of the circle
int count = 0;
for (auto point : points) {
if (dist(point, center) <= radius) {
count++;
}
}
return count;
}
int MinimumRadius(vector<pair<int, int> > points, int k)
{
double left = 0.0;
double right = 0.0;
// Find the maximum distance between any two points
for (int i = 0; i < points.size(); i++) {
for (int j = i + 1; j < points.size(); j++) {
double d = dist(points[i], points[j]);
if (d > right) {
right = d;
}
}
}
while (left <= right) {
double mid = (left + right) / 2.0;
bool found = false;
// Check if there exist k points inside or on the
// circumference of a circle with radius mid
for (int i = 0; i < points.size(); i++) {
if (count_points_in_circle(points, points[i],
mid)
>= k) {
found = true;
break;
}
}
if (found) {
right = mid - 1.0;
}
else {
left = mid + 1.0;
}
}
return static_cast<int>(left);
}
// Example usage
int main()
{
vector<pair<int, int> > points{ { 1, 1 },
{ -1, -1 },
{ 1, -1 } };
int k = 3;
cout << MinimumRadius(points, k) << endl;
}
Java
import java.util.ArrayList;
public class MinimumRadiusProblem {
public static double dist(double[] p1, double[] p2) {
// Calculate Euclidean distance between two points
return Math.sqrt(Math.pow(p1[0] - p2[0], 2) + Math.pow(p1[1] - p2[1], 2));
}
public static int count_points_in_circle(ArrayList<double[]> points, double[] center, double radius) {
// Count the number of points inside or on the circumference of the circle
int count = 0;
for (double[] point : points) {
if (dist(point, center) <= radius) {
count++;
}
}
return count;
}
public static int minimumRadius(ArrayList<double[]> points, int k) {
double left = 0.0;
double right = 0.0;
// Find the maximum distance between any two points
for (int i = 0; i < points.size(); i++) {
for (int j = i + 1; j < points.size(); j++) {
double d = dist(points.get(i), points.get(j));
if (d > right) {
right = d;
}
}
}
while (left <= right) {
double mid = (left + right) / 2.0;
boolean found = false;
// Check if there exist k points inside or on the circumference of a circle with radius mid
for (double[] point : points) {
if (count_points_in_circle(points, point, mid) >= k) {
found = true;
break;
}
}
if (found) {
right = mid - 1.0;
} else {
left = mid + 1.0;
}
}
return (int) Math.floor(left);
}
// Example usage
public static void main(String[] args) {
ArrayList<double[]> points = new ArrayList<>();
points.add(new double[]{1, 1});
points.add(new double[]{-1, -1});
points.add(new double[]{1, -1});
int k = 3;
System.out.println(minimumRadius(points, k));
}
}
Python3
import math
def dist(p1, p2):
# Calculate Euclidean distance between two points
return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)
def count_points_in_circle(points, center, radius):
# Count the number of points inside or on the circumference of the circle
count = 0
for point in points:
if dist(point, center) <= radius:
count += 1
return count
def minimum_radius(points, k):
left, right = 0, 0
# Find the maximum distance between any two points
for i in range(len(points)):
for j in range(i+1, len(points)):
d = dist(points[i], points[j])
if d > right:
right = d
while left <= right:
mid = (left + right) / 2
found = False
# Check if there exist k points inside or on the circumference of a circle with radius mid
for i in range(len(points)):
if count_points_in_circle(points, points[i], mid) >= k:
found = True
break
if found:
right = mid - 1
else:
left = mid + 1
return int(left)
# Example usage
points = [(1, 1), (-1, -1), (1, -1)]
k = 3
print(minimum_radius(points, k))
C#
using System;
using System.Collections.Generic;
public class MinimumRadiusProblem
{
public static double dist(double[] p1, double[] p2)
{
// Calculate Euclidean distance between two points
return Math.Sqrt(Math.Pow(p1[0] - p2[0], 2) + Math.Pow(p1[1] - p2[1], 2));
}
public static int count_points_in_circle(List<double[]> points, double[] center, double radius)
{
// Count the number of points inside or on the circumference of the circle
int count = 0;
foreach (double[] point in points)
{
if (dist(point, center) <= radius)
{
count++;
}
}
return count;
}
public static int minimumRadius(List<double[]> points, int k)
{
double left = 0.0;
double right = 0.0;
// Find the maximum distance between any two points
for (int i = 0; i < points.Count; i++)
{
for (int j = i + 1; j < points.Count; j++)
{
double d = dist(points[i], points[j]);
if (d > right)
{
right = d;
}
}
}
while (left <= right)
{
double mid = (left + right) / 2.0;
bool found = false;
// Check if there exist k points inside or on the circumference of a circle with radius mid
foreach (double[] point in points)
{
if (count_points_in_circle(points, point, mid) >= k)
{
found = true;
break;
}
}
if (found)
{
right = mid - 1.0;
}
else
{
left = mid + 1.0;
}
}
return (int)Math.Floor(left);
}
// Example usage
public static void Main(string[] args)
{
List<double[]> points = new List<double[]>
{
new double[] { 1, 1 },
new double[] { -1, -1 },
new double[] { 1, -1 }
};
int k = 3;
Console.WriteLine(minimumRadius(points, k));
}
}
JavaScript
// JavaScript implementation of Minimum Radius problem
function dist(p1, p2) {
// Calculate Euclidean distance between two points
return Math.sqrt(Math.pow(p1[0] - p2[0], 2) + Math.pow(p1[1] - p2[1], 2));
}
function count_points_in_circle(points, center, radius) {
// Count the number of points inside or on the
// circumference of the circle
let count = 0;
for (let point of points) {
if (dist(point, center) <= radius) {
count++;
}
}
return count;
}
function MinimumRadius(points, k) {
let left = 0.0;
let right = 0.0;
// Find the maximum distance between any two points
for (let i = 0; i < points.length; i++) {
for (let j = i + 1; j < points.length; j++) {
let d = dist(points[i], points[j]);
if (d > right) {
right = d;
}
}
}
while (left <= right) {
let mid = (left + right) / 2.0;
let found = false;
// Check if there exist k points inside or on the
// circumference of a circle with radius mid
for (let i = 0; i < points.length; i++) {
if (count_points_in_circle(points, points[i], mid) >= k) {
found = true;
break;
}
}
if (found) {
right = mid - 1.0;
} else {
left = mid + 1.0;
}
}
return Math.floor(left);
}
// Example usage
const points = [
[1, 1],
[-1, -1],
[1, -1]
];
const k = 3;
console.log(MinimumRadius(points, k));
Time Complexity: O(n^2 * log(r)) where n is the number of points and r is the maximum distance between any two points.
Space complexity: O(1) as it uses only a constant amount of extra space irrespective of the size of the input.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed abo
Similar Reads
Minimum K such that every substring of length atleast K contains a character c
Given a string S containing lowercase latin letters. A character c is called K-amazing if every substring of S with length atleast K contains this character c. Find the minimum possible K such that there exists atleast one K-amazing character. Examples: Input : S = "abcde" Output :3 Explanation : Ev
11 min read
Angular Sweep (Maximum points that can be enclosed in a circle of given radius)
Given ânâ points on the 2-D plane, find the maximum number of points that can be enclosed by a fixed-radius circle of radius âRâ. Note: The point is considered to be inside the circle even when it lies on the circumference. Examples: Input: R = 1 points[] = {(6.47634, 7.69628), (5.16828 4.79915), (
15 min read
Find a point that lies inside exactly K given squares
Given an integer K and an array arr each of whose element x represents a square with two of its vertices as (0, 0) and (x, x). The task is to find a point which lies in exactly K squares. Examples: Input: arr[] = {1, 2, 3, 4}, K = 2 Output: (3, 3) The point (3, 3) lies inside 3rd and 4th square only
3 min read
Find minimum positive integer x such that a(x^2) + b(x) + c >= k
Given four integers a, b, c, and k. The task is to find the minimum positive value of x such that ax2 + bx + c ⥠k. Examples: Input: a = 3, b = 4, c = 5, k = 6 Output: 1 For x = 0, a * 0 + b * 0 + c = 5 < 6 For x = 1, a * 1 + b * 1 + c = 3 + 4 + 5 = 12 > 6 Input: a = 2, b = 7, c = 6, k = 3 Ou
8 min read
Find if a point lies inside a Circle
Given a circle (coordinates of centre and radius) and a point (coordinate), find if the point lies inside or on the circle, or not. Examples : Input: x = 4, y = 4 // Given Point circle_x = 1, circle_y = 1, rad = 6; // Circle Output: Inside Input: x = 3, y = 3 // Given Point circle_x = 0, circle_y =
5 min read
Find if a point lies inside, outside or on the circumcircle of three points A, B, C
Given four non-collinear points A, B, C, and P. The task is to find whether point P lies inside, outside or on the circumcircle formed by points A, B, and C.Examples Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {2, 4}; Output: Point (2, 4) is inside the circumcircle.Input: A = {2, 8}, B = {2, 1},
15+ min read
Find a point such that sum of the Manhattan distances is minimized
Given N points in K dimensional space where 2\leq N\leq 10^{5} and 1\leq K\leq 5 . The task is to determine the point such that the sum of Manhattan distances from this point to the N points is minimized. Manhattan distance is the distance between two points measured along axes at right angles. In a
5 min read
Queries on count of points lie inside a circle
Given n coordinate (x, y) of points on 2D plane and Q queries. Each query contains an integer r, the task is to count the number of points lying inside or on the circumference of the circle having radius r and centered at the origin.Examples : Input : n = 5 Coordinates: 1 1 2 2 3 3 -1 -1 4 4 Query 1
8 min read
Count points from an array that lies inside a semi-circle
Given two pairs (X, Y), (P, Q) and R the coordinate of the center of semi-circle, coordinate of the intersection of semicircle and diameter of the semicircle and, the radius of the semicircle, and an array arr[] of dimension N*2 consisting of the coordinates of few points, the task is to find the nu
7 min read
Minimum volume of cone that can be circumscribed about a sphere of radius R
Given a sphere of radius R, The task is to find out the minimum volume of the cone that can be circumscribed about it. Examples: Input: R = 10 Output: Volume of cone = 8373.33 Explanation: Radius of cone = 14.14 and Height of cone = 40, Volume of cone = (1/3) * \pi * r^2 * hSo, volume = 8373.33Input
5 min read