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: 2Input: L = 8, B = 9
Output: 4Input: 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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the greatest // altitude of the largest triangle // triangle that can be inscribed // in the rectangle int largestAltitude( int L, int B) { // If L is greater than B if (L > B) { swap(B, L); } // Variables to perform binary // search int low = 0, high = L; // Stores the maximum altitude // possible int res = 0; // Iterate until low is less // than high while (low <= high) { // Stores the mid value int mid = low + (high - low) / 2; // If mide is less than // or equal to the B/2 if (mid <= (B / 2)) { // Update res res = mid; // Update low low = mid + 1; } else // Update high high = mid - 1; } // Print the result return res; } // Driver Code int main() { // Given Input int L = 3; int B = 4; // Function call cout << largestAltitude(L, B); return 0; } |
Java
// Java program for the above approach import java.io.*; class GFG { // Function to find the greatest // altitude of the largest triangle // triangle that can be inscribed // in the rectangle static int largestAltitude( int L, int B) { // If L is greater than B if (L > B) { int t = L; L = B; B = t; } // Variables to perform binary // search int low = 0 , high = L; // Stores the maximum altitude // possible int res = 0 ; // Iterate until low is less // than high while (low <= high) { // Stores the mid value int mid = low + (high - low) / 2 ; // If mide is less than // or equal to the B/2 if (mid <= (B / 2 )) { // Update res res = mid; // Update low low = mid + 1 ; } else // Update high high = mid - 1 ; } // Print the result return res; } // Driver Code public static void main(String[] args) { // Given Input int L = 3 ; int B = 4 ; // Function call System.out.print(largestAltitude(L, B)); } } // This code is contributed by splevel62. |
Python3
# Python 3 program for the above approach # Function to find the greatest # altitude of the largest triangle # triangle that can be inscribed # in the rectangle def largestAltitude(L, B): # If L is greater than B if (L > B): temp = B B = L L = temp # Variables to perform binary # search low = 0 high = L # Stores the maximum altitude # possible res = 0 # Iterate until low is less # than high while (low < = high): # Stores the mid value mid = low + (high - low) / / 2 # If mide is less than # or equal to the B/2 if (mid < = (B / 2 )): # Update res res = mid # Update low low = mid + 1 else : # Update high high = mid - 1 # Print the result return res # Driver Code if __name__ = = '__main__' : # Given Input L = 3 B = 4 # Function call print (largestAltitude(L, B)) # This code is contributed by ipg2016107. |
C#
// C# program for the above approach using System; class GFG{ // Function to find the greatest // altitude of the largest triangle // triangle that can be inscribed // in the rectangle static int largestAltitude( int L, int B) { // If L is greater than B if (L > B) { int t = L; L = B; B = t; } // Variables to perform binary // search int low = 0, high = L; // Stores the maximum altitude // possible int res = 0; // Iterate until low is less // than high while (low <= high) { // Stores the mid value int mid = low + (high - low) / 2; // If mide is less than // or equal to the B/2 if (mid <= (B / 2)) { // Update res res = mid; // Update low low = mid + 1; } else // Update high high = mid - 1; } // Print the result return res; } // Driver Code public static void Main( string [] args) { // Given Input int L = 3; int B = 4; // Function call Console.Write(largestAltitude(L, B)); } } // This code is contributed by code_hunt |
Javascript
<script> // JavaScript program for the above approach // Function to find the greatest // altitude of the largest triangle // triangle that can be inscribed // in the rectangle function largestAltitude(L, B) { // If L is greater than B if (L > B) { let temp = B; B = L; L = temp; } // Variables to perform binary // search let low = 0, high = L; // Stores the maximum altitude // possible let res = 0; // Iterate until low is less // than high while (low <= high) { // Stores the mid value let mid = Math.floor(low + (high - low) / 2); // If mide is less than // or equal to the B/2 if (mid <= Math.floor(B / 2)) { // Update res res = mid; // Update low low = mid + 1; } // Update high else high = mid - 1; } // Print the result return res; } // Driver Code // Given Input let L = 3; let B = 4; // Function call document.write(largestAltitude(L, B)); </script> |
2
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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the greatest // altitude of the largest triangle // triangle that can be inscribed // in the rectangle int largestAltitude( int L, int B) { // If L is greater than B if (L > B) swap(L, B); // Stores the maximum altitude // value int res = min(B / 2, L); // Return res return res; } // Driver Code int main() { // Given Input int L = 3; int B = 4; // Function call cout << largestAltitude(L, B); return 0; } |
Java
// C++ program for the above approach import java.io.*; class GFG { // Function to find the greatest // altitude of the largest triangle // triangle that can be inscribed // in the rectangle static int largestAltitude( int L, int B) { // If L is greater than B if (L > B) { int t = L; L = B; B = t; } // Stores the maximum altitude // value int res = Math.min(B / 2 , L); // Return res return res; } // Driver Code public static void main(String[] args) { // Given Input int L = 3 ; int B = 4 ; // Function call System.out.print( largestAltitude(L, B)); } } // This code is contributed by shivanisinghss2110 |
Python3
# Python program for the above approach # Function to find the greatest # altitude of the largest triangle # triangle that can be inscribed # in the rectangle def largestAltitude( L, B): # If L is greater than B if (L > B): temp = B B = L L = temp # Stores the maximum altitude # value res = min (B / / 2 , L) # Return res return res # Driver Code # Given Input L = 3 B = 4 # Function call print (largestAltitude(L, B)) # This code is contributed by shivanisinghss2110 |
C#
// C# program for the above approach using System; class GFG { // Function to find the greatest // altitude of the largest triangle // triangle that can be inscribed // in the rectangle static int largestAltitude( int L, int B) { // If L is greater than B if (L > B) { int t = L; L = B; B = t; } // Stores the maximum altitude // value int res = Math.Min(B / 2, L); // Return res return res; } // Driver Code public static void Main(String[] args) { // Given Input int L = 3; int B = 4; // Function call Console.Write( largestAltitude(L, B)); } } // This code is contributed by shivanisinghss2110 |
Javascript
<script> // JAvascript. program for the above approach // Function to find the greatest // altitude of the largest triangle // triangle that can be inscribed // in the rectangle function largestAltitude(L, B) { // If L is greater than B if (L > B) { var t = L; L = B; B = t; } // Stores the maximum altitude // value var res = Math.min(B / 2, L); // Return res return res; } // Driver Code // Given Input var L = 3; var B = 4; // Function call document.write( largestAltitude(L, B)); // This code is contributed by shivanisinghss2110 </script> |
2
Time Complexity: O(1)
Auxiliary Space: O(1)