Related Articles
Number of jump required of given length to reach a point of form (d, 0) from origin in 2D plane
• Difficulty Level : Medium
• Last Updated : 09 May, 2018

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).

Examples:

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.
How ?
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:

## C++

 `#include ` `using` `namespace` `std; ` ` `  `// Return the minimum jump of length either a or b ` `// required to reach (d, 0) from (0, 0). ` `int` `minJumps(``int` `a, ``int` `b, ``int` `d) ` `{ ` `    ``// Assigning maximum of a and b to b ` `    ``// and assigning minimum of a and b to a. ` `    ``int` `temp = a; ` `    ``a = min(a, b); ` `    ``b = max(temp, b); ` ` `  `    ``// if d is greater than or equal to b. ` `    ``if` `(d >= b) ` `        ``return` `(d + b - 1) / b; ` ` `  `    ``// if d is 0 ` `    ``if` `(d == 0) ` `        ``return` `0; ` ` `  `    ``// if d is equal to a. ` `    ``if` `(d == a) ` `        ``return` `1; ` ` `  `    ``// else make triangle, and only 2  ` `    ``// steps required. ` `    ``return` `2; ` `} ` ` `  `int` `main() ` `{ ` `    ``int` `a = 3, b = 4, d = 11; ` `    ``cout << minJumps(a, b, d) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java code to find the minimum number ` `// of jump required to reach  ` `// (d, 0) from (0, 0). ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``// Return the minimum jump of length either a or b ` `    ``// required to reach (d, 0) from (0, 0). ` `    ``static` `int` `minJumps(``int` `a, ``int` `b, ``int` `d) ` `    ``{ ` `        ``// Assigning maximum of a and b to b ` `        ``// and assigning minimum of a and b to a. ` `        ``int` `temp = a; ` `        ``a = Math.min(a, b); ` `        ``b = Math.max(temp, b); ` ` `  `        ``// if d is greater than or equal to b. ` `        ``if` `(d >= b) ` `            ``return` `(d + b - ``1``) / b; ` ` `  `        ``// if d is 0 ` `        ``if` `(d == ``0``) ` `            ``return` `0``; ` ` `  `        ``// if d is equal to a. ` `        ``if` `(d == a) ` `            ``return` `1``; ` ` `  `        ``// else make triangle, and only 2 ` `        ``// steps required. ` `        ``return` `2``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `a = ``3``, b = ``4``, d = ``11``; ` `        ``System.out.println(minJumps(a, b, d)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m `

## Python3

 `# Python3 code to find the minimum  ` `# number of jump required to reach  ` `# (d, 0) from (0, 0) ` ` `  `def` `minJumps(a, b, d): ` `     `  `    ``temp ``=` `a ` `    ``a ``=` `min``(a, b) ` `    ``b ``=` `max``(temp, b) ` `     `  `    ``if` `(d >``=` `b): ` `        ``return` `(d ``+` `b ``-` `1``) ``/` `b ` `     `  `    ``# if d is 0 ` `    ``if` `(d ``=``=` `0``): ` `        ``return` `0` ` `  `    ``# if d is equal to a. ` `    ``if` `(d ``=``=` `a): ` `        ``return` `1` ` `  `    ``# else make triangle, and  ` `    ``# only 2  steps required. ` `    ``return` `2` `         `  `# Driver Code ` `a, b, d ``=` `3``, ``4``, ``11` `print` `(``int``(minJumps(a, b, d))) ` ` `  `# This code is contributed by _omg `

## C#

 `// C# code to find the minimum number ` `// of jump required to reach  ` `// (d, 0) from (0, 0). ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Return the minimum jump of length either a or b ` `    ``// required to reach (d, 0) from (0, 0). ` `    ``static` `int` `minJumps(``int` `a, ``int` `b, ``int` `d) ` `    ``{ ` `        ``// Assigning maximum of a and b to b ` `        ``// and assigning minimum of a and b to a. ` `        ``int` `temp = a; ` `        ``a = Math.Min(a, b); ` `        ``b = Math.Max(temp, b); ` ` `  `        ``// if d is greater than or equal to b. ` `        ``if` `(d >= b) ` `            ``return` `(d + b - 1) / b; ` ` `  `        ``// if d is 0 ` `        ``if` `(d == 0) ` `            ``return` `0; ` ` `  `        ``// if d is equal to a. ` `        ``if` `(d == a) ` `            ``return` `1; ` ` `  `        ``// else make triangle, and only 2 ` `        ``// steps required. ` `        ``return` `2; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `a = 3, b = 4, d = 11; ` `        ``Console.WriteLine(minJumps(a, b, d)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m `

## PHP

 `= ``\$b``) ` `        ``return` `(``\$d` `+ ``\$b` `- 1) / ``\$b``; ` ` `  `    ``// if d is 0 ` `    ``if` `(``\$d` `== 0) ` `        ``return` `0; ` ` `  `    ``// if d is equal to a. ` `    ``if` `(``\$d` `== ``\$a``) ` `        ``return` `1; ` ` `  `    ``// else make triangle, ` `    ``// and only 2  ` `    ``// steps required. ` `    ``return` `2; ` `} ` ` `  `    ``// Driver Code ` `    ``\$a` `= 3; ` `    ``\$b` `= 4;  ` `    ``\$d` `= 11; ` `    ``echo` `floor``(minJumps(``\$a``, ``\$b``, ``\$d``)); ` `     `  `// This code is contributed by anuj_67. ` `?> `

Output

`3`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}