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++
#include<bits/stdc++.h>
using namespace std;
int minRadius( int k, int x[], int y[], int n)
{
int dis[n];
for ( int i = 0; i < n; i++)
dis[i] = x[i] * x[i] + y[i] * y[i];
sort(dis, dis + n);
return dis[k - 1];
}
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
import java.util.Arrays;
class GFG
{
static int minRadius( int k, int [] x, int [] y,
int n)
{
int [] dis= new int [n];
for ( int i = 0 ; i < n; i++)
dis[i] = x[i] * x[i] + y[i] * y[i];
Arrays.sort(dis);
return dis[k - 1 ];
}
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));
}
}
|
Python3
def minRadius(k, x, y, n):
dis = [ 0 ] * n
for i in range ( 0 , n):
dis[i] = x[i] * x[i] + y[i] * y[i]
dis.sort()
return dis[k - 1 ]
k = 3
x = [ 1 , - 1 , 1 ]
y = [ 1 , - 1 , - 1 ]
n = len (x)
print (minRadius(k, x, y, n))
|
C#
using System;
class GFG {
static int minRadius( int k, int []x,
int [] y, int n)
{
int [] dis = new int [n];
for ( int i = 0; i < n; i++)
dis[i] = x[i] * x[i] +
y[i] * y[i];
Array.Sort(dis);
return dis[k - 1];
}
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));
}
}
|
Javascript
<script>
function minRadius(k, x, y, n) {
let dis = Array.from({length: n}, (_, i) => 0);
for (let i = 0; i < n; i++)
dis[i] = x[i] * x[i] + y[i] * y[i];
dis.sort();
return dis[k - 1];
}
let k = 3;
let x = [ 1, -1, 1 ];
let y = [ 1, -1, -1 ];
let n = x.length;
document.write(minRadius(k, x, y, n));
</script>
|
PHP
<?php
function minRadius( $k , $x , $y , $n )
{
$dis = array ();
for ( $i = 0; $i < $n ; $i ++)
$dis [ $i ] = $x [ $i ] * $x [ $i ] +
$y [ $i ] * $y [ $i ];
sort( $dis );
return $dis [ $k - 1];
}
$k = 3;
$x = array (1, -1, 1);
$y = array (1, -1, -1);
$n = count ( $x );
echo minRadius( $k , $x , $y , $n ) ;
?>
|
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)
{
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)
{
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;
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 ;
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);
}
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) {
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) {
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 ;
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 ;
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);
}
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):
return math.sqrt((p1[ 0 ] - p2[ 0 ]) * * 2 + (p1[ 1 ] - p2[ 1 ]) * * 2 )
def count_points_in_circle(points, center, radius):
count = 0
for point in points:
if dist(point, center) < = radius:
count + = 1
return count
def minimum_radius(points, k):
left, right = 0 , 0
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
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)
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)
{
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)
{
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;
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 ;
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);
}
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
function dist(p1, p2) {
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) {
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;
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 ;
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);
}
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.
This article is contributed by Anuj Chauhan. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed abo
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!