Given two ladders of height **h1** and **h2**. The task is to find the maximum height that can’t be reached using any possible combination by the two ladders. If it is possible to reach all the heights then print **0**.

**Examples:**

Input:H1 = 2, H2 = 11

Output:9

We cannot reach heights 1, 3, 5, 7 and 9.

So, the maximum possible height is 9.

Input:H1 = 7, H2 = 5

Output:23

**Approach:** For the given numbers **a** and **b**, the maximum number **c** such that **ax + by = c** is not possible where **x ≥ 0** and **y ≥ 0** is Frobenius number which is equal to **(a * b) – a – b**.

Below is the implementation of the above approach:

## CPP

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the maximum height ` `// which can't be reached ` `int` `maxHeight(` `int` `h1, ` `int` `h2) ` `{ ` ` ` `return` `((h1 * h2) - h1 - h2); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `h1 = 7, h2 = 5; ` ` ` ` ` `cout << max(0, maxHeight(h1, h2)); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the maximum height ` ` ` `// which can't be reached ` ` ` `static` `int` `maxHeight(` `int` `h1, ` `int` `h2) ` ` ` `{ ` ` ` `return` `((h1 * h2) - h1 - h2); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `h1 = ` `7` `, h2 = ` `5` `; ` ` ` ` ` `System.out.println(Math.max(` `0` `, maxHeight(h1, h2))); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation of the approach ` ` ` `# Function to return the maximum height ` `# which can't be reached ` `def` `maxHeight(h1, h2): ` ` ` `return` `((h1 ` `*` `h2) ` `-` `h1 ` `-` `h2) ` ` ` `# Driver code ` `h1 ` `=` `7` `h2 ` `=` `5` ` ` `print` `(` `max` `(` `0` `, maxHeight(h1, h2))) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the maximum height ` ` ` `// which can't be reached ` ` ` `static` `int` `maxHeight(` `int` `h1, ` `int` `h2) ` ` ` `{ ` ` ` `return` `((h1 * h2) - h1 - h2); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `h1 = 7, h2 = 5; ` ` ` ` ` `Console.WriteLine(Math.Max(0, maxHeight(h1, h2))); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

23

**Time Complexity:** O(1)

## Recommended Posts:

- Maximum height of triangular arrangement of array values
- Find the height of a right-angled triangle whose area is X times its base
- Sqrt (or Square Root) Decomposition | Set 2 (LCA of Tree in O(sqrt(height)) time)
- Count Balanced Binary Trees of Height h
- Traversal of tree with k jumps allowed between nodes of same height
- Radius of the circle when the width and height of an arc is given
- Print the nodes of the Binary Tree whose height is a Prime number
- Percentage increase in the cylinder if the height is increased by given percentage but radius remains constant
- Percentage increase in the volume of cuboid if length, breadth and height are increased by fixed percentages
- Height of Pyramid formed with given Rectangular Box
- Count triangles required to form a House of Cards of height N
- Find Maximum and Minimum of two numbers using Absolute function
- Query to find the maximum and minimum weight between two nodes in the given tree using LCA.
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Program to find the maximum difference between the index of any two different numbers
- Find two equal subsequences of maximum length with at least one different index
- Find two numbers with given sum and maximum possible LCM
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Find area of triangle if two vectors of two adjacent sides are given
- Find the maximum distance covered using n bikes

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.