# Check if a given circle lies completely inside the ring formed by two concentric circles

Given two circles of radius r and R, both have their centre at the origin. Now, given another circle of radius r1 and centre at (x1, y1). Check, if the third circle(circle of radius r1) lies completely inside the ring formed by two circles of radius r and R.
Examples :

```Input : r = 8 R = 4
r1 = 2 x1 = 6 y1 = 0
Output : yes

Input : r = 8 R = 4
r1 = 2 x1 = 5 y1 = 0
Output : no```

Important : Concentric circles are those circles which have same centre. The region lying between two concentric circles is called annulus or the circular ring.

Example :
There are two concentric circles with their centre at origin(0, 0) and radius as r = 8 and R = 4.
1.) Circle 1 and 2 lies inside the ring.
2.) Circle 3 and 4 are outside the ring.
The complete figure can be visualised as given below :

Approach :
This problem can be solved using Pythagoras Theorem . Compute the distance between the centre of the circle and origin using Pythagoras theorem, suppose it is denoted by ‘dis’.
After computing the distance just check that the value of (dis – r1)> = r and (dis + r1)< = R. If both these conditions hold then the circle lies completely inside the ring.

## C++

 `// CPP code to check if a circle ` `// lies in the ring` `#include ` `using` `namespace` `std;`   `// Function to check if circle ` `// lies in the ring` `bool` `checkcircle(``int` `r, ``int` `R, ``int` `r1, ` `                        ``int` `x1, ``int` `y1)` `{` `    ``// distance between center of circle` `    ``// center of concentric circles(origin)` `    ``// using Pythagoras theorem` `    ``int` `dis = ``sqrt``(x1*x1+y1*y1);` `    `  `    ``// Condition to check if circle is` `    ``// strictly inside the ring` `    ``return` `(dis-r1 >= R && dis+r1 <= r);` `}`   `// Driver Code` `int` `main() ` `{` `    ``// Both circle with radius 'r' ` `    ``// and 'R' have center (0,0)` `    ``int` `r = 8, R = 4, r1 = 2, x1 = 6, y1 = 0;    ` `    ``if` `(checkcircle(r, R, r1, x1, y1))` `       ``cout << ``"yes"` `<< endl;` `    ``else` `       ``cout << ``"no"` `<< endl;` `    `  `    ``return` `0;` `}`

## Java

 `// Java code to check if a ` `// circle lies in the ring` `import` `java.io.*;`   `class` `ring` `{` `    ``// Function to check if circle ` `    ``// lies in the ring` `    ``public` `static` `boolean` `checkcircle(``int` `r, ``int` `R,` `                            ``int` `r1, ``int` `x1, ``int` `y1)` `    ``{` `        ``// distance between center of circle` `        ``// center of concentric circles(origin)` `        ``// using Pythagoras theorem` `        ``int` `dis = (``int``)Math.sqrt(x1 * x1 + ` `                                 ``y1 * y1);` `        `  `         ``// Condition to check if circle` `         ``// is strictly inside the ring` `        ``return` `(dis - r1 >= R && dis + r1 <= r);` `    ``}` `    `  `    ``// Driver Code` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``// Both circle with radius 'r' ` `        ``// and 'R' have center (0,0)` `        ``int` `r = ``8``, R = ``4``, r1 = ``2``, x1 = ``6``, y1 = ``0``;` `       `  `        ``if` `(checkcircle(r, R, r1, x1, y1))` `            ``System.out.println(``"yes"``);` `        ``else` `            ``System.out.println(``"no"``);` `    ``}` `}`

## Python3

 `# Python3 code to check if ` `# a circle  lies in the ring` `import` `math`   `# Function to check if circle ` `# lies in the ring` `def` `checkcircle(r, R, r1, x1, y1):`   `    ``# distance between center of circle` `    ``# center of concentric circles(origin)` `    ``# using Pythagoras theorem` `    ``dis ``=` `int``(math.sqrt(x1 ``*` `x1 ``+` `y1 ``*` `y1))` `    `  `    ``# Condition to check if circle is` `    ``# strictly inside the ring` `    ``return` `(dis``-``r1 >``=` `R ``and` `dis``+``r1 <``=` `r)`     `# Driver Code`   `# Both circle with radius 'r' ` `# and 'R' have center (0,0)` `r ``=` `8``; R ``=` `4``; r1 ``=` `2``; x1 ``=` `6``; y1 ``=` `0` `if` `(checkcircle(r, R, r1, x1, y1)):` `    ``print``(``"yes"``)` `else``:` `    ``print``(``"no"``)` `    `  `# This code is contributed by Smitha Dinesh Semwal.`

## C#

 `// C# code to check if a` `// circle lies in the ring` `using` `System;`   `class` `ring {` `    `  `    ``// Function to check if circle` `    ``// lies in the ring` `    ``public` `static` `bool` `checkcircle(``int` `r, ``int` `R,` `                         ``int` `r1, ``int` `x1, ``int` `y1)` `    ``{` `        ``// distance between center of circle` `        ``// center of concentric circles(origin)` `        ``// using Pythagoras theorem` `        ``int` `dis = (``int``)Math.Sqrt(x1 * x1 + y1 * y1);`   `        ``// Condition to check if circle` `        ``// is strictly inside the ring` `        ``return` `(dis - r1 >= R && dis + r1 <= r);` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `Main()` `    ``{` `        ``// Both circle with radius 'r'` `        ``// and 'R' have center (0, 0)` `        ``int` `r = 8, R = 4, r1 = 2, x1 = 6, y1 = 0;`   `        ``if` `(checkcircle(r, R, r1, x1, y1))` `            ``Console.WriteLine(``"yes"``);` `        ``else` `            ``Console.WriteLine(``"no"``);` `    ``}` `}`   `// This code is contributed by vt_m.`

## PHP

 `= ``\$R` `&& ``\$dis` `+ ``\$r1` `<= ``\$r``);` `}`   `    ``// Driver Code` `    ``// Both circle with radius 'r' ` `    ``// and 'R' have center (0,0)` `    ``\$r` `= 8; ``\$R` `= 4; ` `    ``\$r1` `= 2; ``\$x1` `= 6;` `    ``\$y1` `= 0; ` `    ``if` `(checkcircle(``\$r``, ``\$R``, ``\$r1``, ``\$x1``, ``\$y1``))` `    `  `    ``echo` `"yes"` `,``"\n"``;` `    ``else` `    ``echo` `"no"` `,``"\n"``;` `    `  `// This code is contributed by ajit. ` `?>`

## Javascript

 ``

Output:

`yes`

Time Complexity: O(log(n)) since using inbuilt sqrt function

Auxiliary Space: O(1)

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

Previous
Next