Altitude of largest Triangle that can be inscribed in a Rectangle | Java program for the above approach ; Function to find the greatest altitude of the largest triangle triangle that can be inscribed in the rectangle ; If L is greater than B ; Variables to perform binary search ; Stores the maximum altitude possible ; Iterate until low is less than high ; Stores the mid value ; If mide is less than or equal to the B / 2 ; Update res ; Update low ; Update high ; Print the result ; Driver Code ; Given Input ; Function call
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 ) ) ; } }