Given two integers r1 and r2, representing the radius of two circles, the task is to find the radius of the circle having area equal to the sum of the area of the two circles having given radii.
Examples:
Input:
r1 = 8
r2 = 6
Output:
10
Explanation:
Area of circle with radius 8 = 201.061929
Area of a circle with radius 6 = 113.097335
Area of a circle with radius 10 = 314.159265
Input:
r1 = 2
r2 = 2
Output:
2.82843
Approach: Follow the steps below to solve the problem:
- Calculate area of the first circle is a1 = 3.14 * r1 * r1.
- Calculate area of the second circle is a2 = 3.14 * r2 * r2.
- Therefore, area of the third circle is a1 + a2.
- Radius of the third circle is sqrt(a3 / 3.14)
Below is the implementation of the following approach.
C++14
#include <bits/stdc++.h>
using namespace std;
double findRadius( double r1, double r2)
{
double a1, a2, a3, r3;
a1 = 3.14 * r1 * r1;
a2 = 3.14 * r2 * r2;
a3 = a1 + a2;
r3 = sqrt (a3 / 3.14);
return r3;
}
int main()
{
double r1 = 8, r2 = 6;
cout << findRadius(r1, r2);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static double findRadius( double r1, double r2)
{
double a1, a2, a3, r3;
a1 = 3.14 * r1 * r1;
a2 = 3.14 * r2 * r2;
a3 = a1 + a2;
r3 = Math.sqrt(a3 / 3.14 );
return r3;
}
public static void main(String[] args)
{
double r1 = 8 , r2 = 6 ;
System.out.println(( int )findRadius(r1, r2));
}
}
|
Python3
def findRadius(r1, r2):
a1, a2, a3, r3 = 0 , 0 , 0 , 0 ;
a1 = 3.14 * r1 * r1;
a2 = 3.14 * r2 * r2;
a3 = a1 + a2;
r3 = ((a3 / 3.14 ) * * ( 1 / 2 ));
return r3;
if __name__ = = '__main__' :
r1 = 8 ; r2 = 6 ;
print ( int (findRadius(r1, r2)));
|
C#
using System;
class GFG
{
static double findRadius( double r1, double r2)
{
double a1, a2, a3, r3;
a1 = 3.14 * r1 * r1;
a2 = 3.14 * r2 * r2;
a3 = a1 + a2;
r3 = Math.Sqrt(a3 / 3.14);
return r3;
}
static void Main()
{
double r1 = 8, r2 = 6;
Console.WriteLine(( int )findRadius(r1, r2));
}
}
|
Javascript
<script>
function findRadius(r1, r2)
{
let a1, a2, a3, r3;
a1 = 3.14 * r1 * r1;
a2 = 3.14 * r2 * r2;
a3 = a1 + a2;
r3 = Math.sqrt(a3 / 3.14);
return r3;
}
let r1 = 8, r2 = 6;
document.write(findRadius(r1, r2));
</script>
|
Time Complexity: O(log(a3)), time complexity of the inbuilt sqrt() function is logn.
Space Complexity: O(1) as constant space is being used.
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!