Skip to content
Related Articles

Related Articles

Improve Article
Find minimum radius such that atleast k point lie inside the circle
  • Difficulty Level : Easy
  • Last Updated : 29 Apr, 2021

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 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




<?php
// PHP 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.
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.
?>

Javascript




<script>
// Javascript program to find minimum radius
// such that atleast k point lie inside
// the circle
 
    // Return minumum distance required so that
    // aleast k polet lie inside the circle.
    function minRadius(k, x, y, n) {
     
        let dis = Array.from({length: n}, (_, i) => 0);
       
        // Finding distance between of each
        // polet 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>   

Output: 
 

2

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks 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 above.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live




My Personal Notes arrow_drop_up
Recommended Articles
Page :