Find maximum unreachable height using two ladders
Last Updated :
25 Sep, 2022
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
#include <bits/stdc++.h>
using namespace std;
int maxHeight( int h1, int h2)
{
return ((h1 * h2) - h1 - h2);
}
int main()
{
int h1 = 7, h2 = 5;
cout << max(0, maxHeight(h1, h2));
return 0;
}
|
Java
class GFG
{
static int maxHeight( int h1, int h2)
{
return ((h1 * h2) - h1 - h2);
}
public static void main(String args[])
{
int h1 = 7 , h2 = 5 ;
System.out.println(Math.max( 0 , maxHeight(h1, h2)));
}
}
|
Python
def maxHeight(h1, h2):
return ((h1 * h2) - h1 - h2)
h1 = 7
h2 = 5
print ( max ( 0 , maxHeight(h1, h2)))
|
C#
using System;
class GFG
{
static int maxHeight( int h1, int h2)
{
return ((h1 * h2) - h1 - h2);
}
public static void Main()
{
int h1 = 7, h2 = 5;
Console.WriteLine(Math.Max(0, maxHeight(h1, h2)));
}
}
|
Javascript
<script>
function maxHeight( h1, h2)
{
return ((h1 * h2) - h1 - h2);
}
var h1 = 7, h2 = 5;
document.write(Math.max(0, maxHeight(h1, h2)));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...