Given three positive integers a, b and d. You are currently at origin (0, 0) on infinite 2D coordinate plane. You are allowed to jump on any point in the 2D plane at euclidean distance either equal to a or b from your current position. The task is to find the minimum number of jump required to reach (d, 0) from (0, 0).
Input : a = 2, b = 3, d = 1 Output : 2 First jump of length a = 2, (0, 0) -> (1/2, √15/2) Second jump of length a = 2, (1/2, √15/2) -> (1, 0) Thus, only two jump are required to reach (1, 0) from (0, 0). Input : a = 3, b = 4, d = 11 Output : 3 (0, 0) -> (4, 0) using length b = 4 (4, 0) -> (8, 0) using length b = 4 (8, 0) -> (11, 0) using length a = 3
First, observe we don’t need to find the intermediate points, we only want the minimum number of jumps required.
So, from (0, 0) we will move in direction of (d, 0) i.e vertically with the jump of length equal to max(a, b) until the Euclidean distance between the current position coordinate and (d, 0) either became less than max(a, b) or equal to 0. This will increase the minimum number of jumps required by 1 at each jump. So this value can be found by floor(d/max(a, b)).
Now, for the rest of the distance, if (d, 0) is a Euclidean distance away, we will make one jump of length a and reach the (d, 0). So, the minimum number of jumps required will be increased by 1 in this case.
Now, let’s solve the case if rest of the distance is not equal to a. Let the rest of distance left be x. Also, observe x will be greater than 0 and less than max(a, b), therefore, 0 < x < 2*max(a, b). We can reach to (d, 0) in 2 steps.
Lets try to build a triangle ABC such that A is our current position, B is target position i.e (d, 0) and C will be the point such that AC = BC = max(a, b). And this is possible to triangle because sum of two side AC + BC is greater than third side AB. So, one jump is from A to C and another jump from C to B.
Below is the implementation of this approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Lexicographically Kth smallest way to reach given coordinate from origin
- Mirror of a point through a 3 D plane
- Distance between a point and a Plane in 3 D
- Shortest distance between a Line and a Point in a 3-D plane
- Find mirror image of a point in 2-D plane
- Find the foot of perpendicular of a point in a 3 D plane
- Find foot of perpendicular from a point in 2 D plane to a Line
- Minimum number of blocks required to form Hollow Rectangular Prism
- Number of squares of side length required to cover an N*M rectangle
- Length of the normal from origin on a straight line whose intercepts are given
- Number of triangles in a plane if no more than two points are collinear
- Number of Triangles that can be formed given a set of lines in Euclidean Plane
- Maximum number of region in which N non-parallel lines can divide a plane
- Reflection of a point at 180 degree rotation of another point
- Count number of pairs of lines intersecting at a Point
- Find the number of points that have atleast 1 point above, below, left or right of it
- Minimum number of Circular obstacles required to obstruct the path in a Grid
- Minimum number of square tiles required to fill the rectangular floor
- Check if a line passes through the origin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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 : vt_m