Altitude of largest Triangle that can be inscribed in a Rectangle
Given a rectangle of length L and breadth B, the task is to print the maximum integer altitude possible of the largest triangle that can be inscribed in it, such that the altitude of the triangle should be equal to half of the base.
Examples:
Input: L = 3, B = 4
Output: 2
Input: L = 8, B = 9
Output: 4
Input: L = 325, B = 300
Output: 162
Naive Approach: The simplest approach is to iterate over the range [0, min(L, B)] in reverse and if the current value is less than or equal to max(L, B) / 2, then print the current value as the answer and break the loop.
Time Complexity: O(min(L, B))
Auxiliary Space: O(1)
Binary Search Approach: The above approach can be optimized by using the Binary Search technique and observing the fact that it is always optimal to select the base of the triangle on the side with a maximum side length of the rectangle. Follow the steps below to solve the problem:
- If L is larger than B, then swap the values.
- Initialize three variables, say, low as 0, and high as L to perform the binary search on the range [0, L].
- Also, initialize a variable, say res as 0 to store the maximum possible length of the altitude.
- Iterate while low is less than or equal to high and perform the following steps:
- Initialize a variable, say mid, and set it to low + (high – low) / 2.
- If the value of mid ? B / 2, then assign mid to res and mid +1 to low.
- Otherwise, set high to mid – 1.
- Finally, after completing the above steps, print the value obtained in res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int largestAltitude( int L, int B)
{
if (L > B) {
swap(B, L);
}
int low = 0, high = L;
int res = 0;
while (low <= high) {
int mid = low + (high - low) / 2;
if (mid <= (B / 2)) {
res = mid;
low = mid + 1;
}
else
high = mid - 1;
}
return res;
}
int main()
{
int L = 3;
int B = 4;
cout << largestAltitude(L, B);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int largestAltitude( int L, int B)
{
if (L > B)
{
int t = L;
L = B;
B = t;
}
int low = 0 , high = L;
int res = 0 ;
while (low <= high)
{
int mid = low + (high - low) / 2 ;
if (mid <= (B / 2 ))
{
res = mid;
low = mid + 1 ;
}
else
high = mid - 1 ;
}
return res;
}
public static void main(String[] args)
{
int L = 3 ;
int B = 4 ;
System.out.print(largestAltitude(L, B));
}
}
|
Python3
def largestAltitude(L, B):
if (L > B):
temp = B
B = L
L = temp
low = 0
high = L
res = 0
while (low < = high):
mid = low + (high - low) / / 2
if (mid < = (B / 2 )):
res = mid
low = mid + 1
else :
high = mid - 1
return res
if __name__ = = '__main__' :
L = 3
B = 4
print (largestAltitude(L, B))
|
C#
using System;
class GFG{
static int largestAltitude( int L, int B)
{
if (L > B)
{
int t = L;
L = B;
B = t;
}
int low = 0, high = L;
int res = 0;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (mid <= (B / 2))
{
res = mid;
low = mid + 1;
}
else
high = mid - 1;
}
return res;
}
public static void Main( string [] args)
{
int L = 3;
int B = 4;
Console.Write(largestAltitude(L, B));
}
}
|
Javascript
<script>
function largestAltitude(L, B) {
if (L > B) {
let temp = B;
B = L;
L = temp;
}
let low = 0,
high = L;
let res = 0;
while (low <= high) {
let mid = Math.floor(low + (high - low) / 2);
if (mid <= Math.floor(B / 2)) {
res = mid;
low = mid + 1;
}
else high = mid - 1;
}
return res;
}
let L = 3;
let B = 4;
document.write(largestAltitude(L, B));
</script>
|
Time Complexity: O(log(min(L, B)))
Auxiliary Space: O(1)
Efficient Approach: The above approach can be further optimized by the observation that by placing the base of the triangle on the side of the length, max(L, B) the maximum altitude will be equal to min(max(L, B)/2, min(L, B)). Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int largestAltitude( int L, int B)
{
if (L > B)
swap(L, B);
int res = min(B / 2, L);
return res;
}
int main()
{
int L = 3;
int B = 4;
cout << largestAltitude(L, B);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int largestAltitude( int L, int B)
{
if (L > B)
{
int t = L;
L = B;
B = t;
}
int res = Math.min(B / 2 , L);
return res;
}
public static void main(String[] args)
{
int L = 3 ;
int B = 4 ;
System.out.print( largestAltitude(L, B));
}
}
|
Python3
def largestAltitude( L, B):
if (L > B):
temp = B
B = L
L = temp
res = min (B / / 2 , L)
return res
L = 3
B = 4
print (largestAltitude(L, B))
|
C#
using System;
class GFG
{
static int largestAltitude( int L, int B)
{
if (L > B)
{
int t = L;
L = B;
B = t;
}
int res = Math.Min(B / 2, L);
return res;
}
public static void Main(String[] args)
{
int L = 3;
int B = 4;
Console.Write( largestAltitude(L, B));
}
}
|
Javascript
<script>
function largestAltitude(L, B)
{
if (L > B)
{
var t = L;
L = B;
B = t;
}
var res = Math.min(B / 2, L);
return res;
}
var L = 3;
var B = 4;
document.write( largestAltitude(L, B));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
10 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...