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:
// 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;
} |
// 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 |
# 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 |
// 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 |
<script> // Javascript implementation of the approach // Function to return the maximum height // which can't be reached function maxHeight( h1, h2)
{ return ((h1 * h2) - h1 - h2);
} var h1 = 7, h2 = 5;
document.write(Math.max(0, maxHeight(h1, h2)));
//This code is contributed by SoumikMondal </script> |
23
Time Complexity: O(1)
Auxiliary Space: O(1)