Largest hexagon that can be inscribed within an equilateral triangle
Last Updated :
25 Jun, 2022
Given an equilateral triangle of side length a, the task is to find the largest hexagon that can be inscribed within it.
Examples:
Input: a = 6
Output: 2
Input: a = 9
Output: 3
Approach: From the figure, it is clear that the three small triangles are also equilateral. So they will have side length b = a / 3 where b is also the length of the hexagon and a is the length of the given equilateral triangle.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float hexagonside( float a)
{
if (a < 0)
return -1;
float x = a / 3;
return x;
}
int main()
{
float a = 6;
cout << hexagonside(a) << endl;
return 0;
}
|
Java
class CLG
{
static float hexagonside( float a)
{
if (a < 0 )
return - 1 ;
float x = a / 3 ;
return x;
}
public static void main(String[] args)
{
float a = 6 ;
System.out.println(hexagonside(a));
}
}
|
Python3
def hexagonside(a):
if a < 0 :
return - 1
x = a / / 3
return x
a = 6
print (hexagonside(a))
|
C#
using System;
class CLG
{
static float hexagonside( float a)
{
if (a < 0)
return -1;
float x = a / 3;
return x;
}
public static void Main()
{
float a = 6;
Console.Write(hexagonside(a));
}
}
|
PHP
<?php
function hexagonside( $a )
{
if ( $a < 0)
return -1;
$x = $a / 3;
return $x ;
}
$a = 6;
echo hexagonside( $a ) ;
?>
|
Javascript
<script>
function hexagonside(a)
{
if (a < 0)
return -1;
var x = a / 3;
return x;
}
var a = 6;
document.write(hexagonside(a));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...