Find minimum radius such that atleast k point lie inside the circle
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
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. ?> |
2
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)); |
2
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
Contact Us