Find maximum unreachable height using two ladders

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

23

Time Complexity: O(1)

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : mohit kumar 29, AnkitRai01