There are 4 circles with positive integer radius r1, r2, r3 and r4 as shown in the figure below.

The task is to find the radius r4 of the circle formed by three circles when radius r1, r2, r3 are given.
(Note that the circles in the picture above are tangent to each other.)
Examples:
Input: r1 = 1, r2 = 1, r3 = 1
Output: 0.154701
Input: r1 = 23, r2 = 46, r3 = 69
Output: 6.000000
Approach 1: (Using Binary Search) :
- The policy is to join the centers of all the circles and form 4 triangles
- After the triangles are formed, equate the sum of areas of the three smaller triangles with the main triangle as far as possible using binary search.
Analysis of the mentioned approach:
- This method works because initially there are 4 triangles as pointed in the above image.
- The main triangle with sides
and the three smaller triangles with sides
.
- The main triangle consists of the small ones so the area of the main triangle is the sum of the areas of the smaller ones.
Forming a search space:
Here binary search. The value for r can be chosen and the sum of the areas of all three smaller triangles can be computed and compared with the area of the main triangle.
- Choosing lower bound

- Choosing upper bound

By intuition, the upper bound value for r4 as the radius of the inscribed circle into the
triangle is less than:
rupper_bound 
Now Binary Search can be applied at the following search space.
Below is the implementation of the problem using the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
double r1, r2, r3;
double area( double a, double b, double c)
{
double p = (a + b + c) / 2;
return sqrt (p) * sqrt (p - a) * sqrt (p - b) * sqrt (p - c);
}
double binary_search()
{
double s = area(r1 + r2, r2 + r3, r3 + r1);
double l = 0, h = s / (r1 + r2 + r3);
while (h - l >= 1.e-7) {
double mid = (l + h) / 2;
double s1 = area(mid + r1, mid + r2, r1 + r2);
double s2 = area(mid + r1, mid + r3, r1 + r3);
double s3 = area(mid + r2, mid + r3, r2 + r3);
if (s1 + s2 + s3 < s) {
l = mid;
}
else {
h = mid;
}
}
return (l + h) / 2;
}
int main()
{
r1 = 1.0;
r2 = 2.0;
r3 = 3.0;
cout << fixed << setprecision(6) << binary_search() << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static double r1, r2, r3;
static double area( double a, double b, double c)
{
double p = (a + b + c) / 2 ;
return Math.sqrt(p) * Math.sqrt(p - a) *
Math.sqrt(p - b) * Math.sqrt(p - c);
}
static double binary_search()
{
double s = area(r1 + r2, r2 + r3, r3 + r1);
double l = 0 , h = s / (r1 + r2 + r3);
while (h - l >= 1 .e- 7 )
{
double mid = (l + h) / 2 ;
double s1 = area(mid + r1, mid + r2, r1 + r2);
double s2 = area(mid + r1, mid + r3, r1 + r3);
double s3 = area(mid + r2, mid + r3, r2 + r3);
if (s1 + s2 + s3 < s)
{
l = mid;
}
else
{
h = mid;
}
}
return (l + h) / 2 ;
}
public static void main(String[] args)
{
r1 = 1.0 ;
r2 = 2.0 ;
r3 = 3.0 ;
System.out.printf( "%.6f" , binary_search());
}
}
|
Python3
import math
r1 = 0
r2 = 0
r3 = 0
def area(a, b, c):
p = (a + b + c) / 2
return ((math.sqrt(p)) *
(math.sqrt(p - a)) *
(math.sqrt(p - b)) *
(math.sqrt(p - c)))
def binary_search():
global r1, r2, r3
s = area(r1 + r2, r2 + r3, r3 + r1)
l = 0
h = s / (r1 + r2 + r3)
while (h - l > 0.00000001 ):
mid = (l + h) / 2
s1 = area(mid + r1, mid + r2, r1 + r2)
s2 = area(mid + r1, mid + r3, r1 + r3)
s3 = area(mid + r2, mid + r3, r2 + r3)
if (s1 + s2 + s3 < s):
l = mid
else :
h = mid
return ((l + h) / 2 )
r1 = 1
r2 = 2
r3 = 3
print ( "{0:.6f}" . format (binary_search()))
|
C#
using System;
class GFG
{
static double r1, r2, r3;
static double area( double a, double b, double c)
{
double p = (a + b + c) / 2;
return Math.Sqrt(p) * Math.Sqrt(p - a) *
Math.Sqrt(p - b) * Math.Sqrt(p - c);
}
static double binary_search()
{
double s = area(r1 + r2, r2 + r3, r3 + r1);
double l = 0, h = s / (r1 + r2 + r3);
while (h - l > 0.00000001)
{
double mid = (l + h) / 2;
double s1 = area(mid + r1, mid + r2, r1 + r2);
double s2 = area(mid + r1, mid + r3, r1 + r3);
double s3 = area(mid + r2, mid + r3, r2 + r3);
if (s1 + s2 + s3 < s)
{
l = mid;
}
else
{
h = mid;
}
}
return (l + h) / 2;
}
public static void Main(String[] args)
{
r1 = 1.0;
r2 = 2.0;
r3 = 3.0;
Console.Write( "{0:F6}" , binary_search());
}
}
|
Javascript
<script>
let r1, r2, r3;
function area(a, b, c)
{
let p = (a + b + c) / 2;
return Math.sqrt(p) * Math.sqrt(p - a) *
Math.sqrt(p - b) * Math.sqrt(p - c);
}
function binary_search()
{
let s = area(r1 + r2, r2 + r3, r3 + r1);
let l = 0, h = s / (r1 + r2 + r3);
while (h - l >= 1.e-7)
{
let mid = (l + h) / 2;
let s1 = area(mid + r1, mid + r2, r1 + r2);
let s2 = area(mid + r1, mid + r3, r1 + r3);
let s3 = area(mid + r2, mid + r3, r2 + r3);
if (s1 + s2 + s3 < s)
{
l = mid;
}
else
{
h = mid;
}
}
return (l + h) / 2;
}
r1 = 1.0;
r2 = 2.0;
r3 = 3.0;
document.write(binary_search().toPrecision(6));
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Approach 2: (Using Descartes’ Theorem)
- According to Descartes’ Theorem, the reciprocals of radii, or “curvatures”, of these circles
satisfy the following relation.

- If
are known, one can solve for, 
- On solving the above equation;

Below is the implementation of the problem using the above formula.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
double r1, r2, r3;
r1 = 1;
r2 = 2;
r3 = 3;
double r4 = (r1 * r2 * r3)
/ (r1 * r2 + r2 * r3 + r1 * r3
+ 2.0 * sqrt (r1 * r2 * r3 * (r1 + r2 + r3)));
cout << fixed << setprecision(6) << r4 << '\n' ;
return 0;
}
|
Java
class GFG
{
public static void main(String[] args)
{
double r1, r2, r3;
r1 = 1 ;
r2 = 2 ;
r3 = 3 ;
double r4 = (r1 * r2 * r3) /
(r1 * r2 + r2 * r3 + r1 * r3 + 2.0 *
Math.sqrt(r1 * r2 * r3 * (r1 + r2 + r3)));
System.out.printf( "%.6f" , r4);
}
}
|
Python3
from math import sqrt
r1 = 1
r2 = 2
r3 = 3
r4 = (r1 * r2 * r3) / (r1 * r2 + r2 * r3 + r1 * r3
+ 2.0 * sqrt(r1 * r2 * r3 * (r1 + r2 + r3)))
print ( round (r4, 6 ))
|
C#
using System;
class GFG
{
public static void Main(String[] args)
{
double r1, r2, r3;
r1 = 1;
r2 = 2;
r3 = 3;
double r4 = (r1 * r2 * r3) /
(r1 * r2 + r2 * r3 + r1 * r3 + 2.0 *
Math.Sqrt(r1 * r2 * r3 * (r1 + r2 + r3)));
Console.Write( "{0:F6}" , r4);
}
}
|
Javascript
<script>
let r1, r2, r3;
r1 = 1;
r2 = 2;
r3 = 3;
let r4 = (r1 * r2 * r3) / (r1 * r2 + r2 *
r3 + r1 * r3 + 2.0 * Math.sqrt(
r1 * r2 * r3 * (r1 + r2 + r3)));
document.write(r4.toFixed(6) + "<br>" );
</script>
|
Time Complexity: O(log(n)) as inbuilt sqrt function is being used which has time complexity of log(n)
Auxiliary Space: O(1)
Reference :
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!
Last Updated :
07 Aug, 2022
Like Article
Save Article