# Number of steps required to reach point (x,y) from (0,0) using zig-zag way

Given a coordinate **(x, y)**. The task is to calculate the number of steps required to reach point (x, y) from (0, 0) using zig-zag way and you cannot travel in straight line for more than 1 unit. Also, start moving along Y axis.

For example we can reach the Point denoted by red color in the respective ways as shown in the below diagram:

**Examples:**

Input:x = 4, y = 4Output:8 In the diagram above the line is passing using 8 steps.Input:x = 4, y = 3Output:9Input:x = 2, y = 1Output:5

**Approach**: By sketching a small diagram we can see the two cases:

**Case 1**: If x is less than y then answer will always be**x + y + 2*((y-x)/2)**.**Case 2**: If x is greater than equal to y then answer will always be**x + y + 2*(((x-y)+1)/2)**.

Below is the implementation of the above approach:

## C++

`// C++ program to find the number of steps ` `// required to reach (x, y) from (0, 0) following ` `// a zig-zag path ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the required position ` `int` `countSteps(` `int` `x, ` `int` `y) ` `{ ` ` ` `if` `(x < y) { ` ` ` `return` `x + y + 2 * ((y - x) / 2); ` ` ` `} ` ` ` `else` `{ ` ` ` `return` `x + y + 2 * (((x - y) + 1) / 2); ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `x = 4, y = 3; ` ` ` `cout << countSteps(x, y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the number of steps ` `// required to reach (x, y) from (0, 0) following ` `// a zig-zag path ` ` ` `class` `GfG ` `{ ` ` ` `// Function to return the required position ` `static` `int` `countSteps(` `int` `x, ` `int` `y) ` `{ ` ` ` `if` `(x < y) ` ` ` `{ ` ` ` `return` `x + y + ` `2` `* ((y - x) / ` `2` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `return` `x + y + ` `2` `* (((x - y) + ` `1` `) / ` `2` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `x = ` `4` `, y = ` `3` `; ` ` ` `System.out.println(countSteps(x, y)); ` `} ` `} ` ` ` `// This code is contributed by Prerna Saini ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the number of ` `# steps required to reach (x, y) from ` `# (0, 0) following a zig-zag path ` ` ` `# Function to return the required position ` `def` `countSteps(x, y): ` ` ` ` ` `if` `x < y: ` ` ` `return` `x ` `+` `y ` `+` `2` `*` `((y ` `-` `x) ` `/` `/` `2` `) ` ` ` ` ` `else` `: ` ` ` `return` `x ` `+` `y ` `+` `2` `*` `(((x ` `-` `y) ` `+` `1` `) ` `/` `/` `2` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `x, y ` `=` `4` `, ` `3` ` ` `print` `(countSteps(x, y)) ` ` ` `# This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the number of steps ` `// required to reach (x, y) from (0, 0) ` `// following a zig-zag path ` `using` `System; ` ` ` `class` `GfG ` `{ ` ` ` `// Function to return the required position ` `static` `int` `countSteps(` `int` `x, ` `int` `y) ` `{ ` ` ` `if` `(x < y) ` ` ` `{ ` ` ` `return` `x + y + 2 * ((y - x) / 2); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `return` `x + y + 2 * (((x - y) + 1) / 2); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `x = 4, y = 3; ` ` ` `Console.WriteLine(countSteps(x, y)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

9

**Time Complexity:** O(1)

## Recommended Posts:

- Minimize the number of steps required to reach the end of the array | Set 2
- Number of jump required of given length to reach a point of form (d, 0) from origin in 2D plane
- Find the minimum number of steps to reach M from N
- Minimum number of moves required to reach the destination by the king in a chess board
- Probability of reaching a point with 2 or 3 steps at a time
- Minimum steps to come back to starting point in a circular tour
- Minimum cost to reach a point N from 0 with two different operations allowed
- Steps required to visit M points in order on a circular ring of N points
- Minimum time to reach a point with +t and -t moves at time t
- Find the number of jumps to reach X in the number line from zero
- Find the number of stair steps
- Largest number N which can be reduced to 0 in K steps
- Print steps to make a number in form of 2^X - 1
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Number of steps to convert to prime factors

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.