Given an integers X. The task is to find the number of jumps to reach a point X in the number line starting from zero.

**Note**: The first jump made can be of length one unit and each successive jump will be exactly one unit longer than the previous jump in length. It is allowed to go either left or right in each jump.

**Examples:**

Input :X = 8Output :4Explanation :0 -> -1 -> 1 -> 4-> 8 are possible stages.Input :X = 9Output :5Explanation :0 -> -1 -> -3 -> 0 -> 4-> 9 are possible stages

**Approach **: On observing carefully, it can be said easily that:

- If you have always jumped in the right direction then after
**n**jumps you will be at the point**p**= 1 + 2 + 3 + 4 + … + n. - If instead of jumping right, you jumped left in the kth jump, you would be at point
.*p – 2k* - Moreover, by carefully choosing which jumps to go left and which to go right, after n jumps, you can be at any point between n * (n + 1) / 2 and – (n * (n + 1) / 2) with the same parity as n * (n + 1) / 2.

Keeping the above points in mind, what you must do is simulate the jumping process, always jumping to the right, and if at some point, you’ve reached a point that has the same parity as X and is at or beyond X, you’ll have your answer.

Below is the implementation of the above approach:

## C++

`// C++ program to find the number of jumps ` `// to reach X in the number line from zero ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Utitlity function to calculate sum ` `// of numbers from 1 to x ` `int` `getsum(` `int` `x) ` `{ ` ` ` `return` `(x * (x + 1)) / 2; ` `} ` ` ` `// Function to find the number of jumps ` `// to reach X in the number line from zero ` `int` `countJumps(` `int` `n) ` `{ ` ` ` `// First make number positive ` ` ` `// Answer will be same either it is ` ` ` `// Positive or negative ` ` ` `n = ` `abs` `(n); ` ` ` ` ` `// To store required answer ` ` ` `int` `ans = 0; ` ` ` ` ` `// Continue till number is lesser or not in same parity ` ` ` `while` `(getsum(ans) < n or (getsum(ans) - n) & 1) ` ` ` `ans++; ` ` ` ` ` `// Return the required answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 9; ` ` ` ` ` `cout << countJumps(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the number of jumps ` `// to reach X in the number line from zero ` ` ` `class` `GFG ` `{ ` ` ` `// Utitlity function to calculate sum ` `// of numbers from 1 to x ` `static` `int` `getsum(` `int` `x) ` `{ ` ` ` `return` `(x * (x + ` `1` `)) / ` `2` `; ` `} ` ` ` `// Function to find the number of jumps ` `// to reach X in the number line from zero ` `static` `int` `countJumps(` `int` `n) ` `{ ` ` ` `// First make number positive ` ` ` `// Answer will be same either it is ` ` ` `// Positive or negative ` ` ` `n = Math.abs(n); ` ` ` ` ` `// To store required answer ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// Continue till number is lesser ` ` ` `// or not in same parity ` ` ` `while` `(getsum(ans) < n || ` ` ` `((getsum(ans) - n) & ` `1` `) > ` `0` `) ` ` ` `ans++; ` ` ` ` ` `// Return the required answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `n = ` `9` `; ` ` ` ` ` `System.out.println(countJumps(n)); ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find the number of jumps ` `# to reach X in the number line from zero ` ` ` `# Utitlity function to calculate sum ` `# of numbers from 1 to x ` `def` `getsum(x): ` ` ` `return` `int` `((x ` `*` `(x ` `+` `1` `)) ` `/` `2` `) ` ` ` `# Function to find the number of jumps ` `# to reach X in the number line from zero ` `def` `countJumps(n): ` ` ` ` ` `# First make number positive ` ` ` `# Answer will be same either it is ` ` ` `# Positive or negative ` ` ` `n ` `=` `abs` `(n) ` ` ` ` ` `# To store the required answer ` ` ` `ans ` `=` `0` ` ` ` ` `# Continue till number is lesser ` ` ` `# or not in same parity ` ` ` `while` `(getsum(ans) < n ` `or` ` ` `(getsum(ans) ` `-` `n) & ` `1` `): ` ` ` `ans ` `+` `=` `1` ` ` ` ` `# Return the required answer ` ` ` `return` `ans ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `9` ` ` ` ` `print` `(countJumps(n)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the number of jumps ` `// to reach X in the number line from zero ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Utitlity function to calculate sum ` `// of numbers from 1 to x ` `static` `int` `getsum(` `int` `x) ` `{ ` ` ` `return` `(x * (x + 1)) / 2; ` `} ` ` ` `// Function to find the number of jumps ` `// to reach X in the number line from zero ` `static` `int` `countJumps(` `int` `n) ` `{ ` ` ` `// First make number positive ` ` ` `// Answer will be same either it is ` ` ` `// Positive or negative ` ` ` `n = Math.Abs(n); ` ` ` ` ` `// To store required answer ` ` ` `int` `ans = 0; ` ` ` ` ` `// Continue till number is lesser or not in same parity ` ` ` `while` `(getsum(ans) < n || ((getsum(ans) - n) & 1)>0) ` ` ` `ans++; ` ` ` ` ` `// Return the required answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `n = 9; ` ` ` ` ` `Console.WriteLine(countJumps(n)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the number of jumps ` `// to reach X in the number line from zero ` ` ` `// Utitlity function to calculate sum ` `// of numbers from 1 to x ` `function` `getsum(` `$x` `) ` `{ ` ` ` `return` `(` `$x` `* (` `$x` `+ 1)) / 2; ` `} ` ` ` `// Function to find the number of jumps ` `// to reach X in the number line from zero ` `function` `countJumps(` `$n` `) ` `{ ` ` ` `// First make number positive ` ` ` `// Answer will be same either it is ` ` ` `// Positive or negative ` ` ` `$n` `= ` `abs` `(` `$n` `); ` ` ` ` ` `// To store required answer ` ` ` `$ans` `= 0; ` ` ` ` ` `// Continue till number is lesser ` ` ` `// or not in same parity ` ` ` `while` `(getsum(` `$ans` `) < ` `$n` `or` ` ` `(getsum(` `$ans` `) - ` `$n` `) & 1) ` ` ` `$ans` `++; ` ` ` ` ` `// Return the required answer ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver code ` `$n` `= 9; ` ` ` `echo` `countJumps(` `$n` `); ` ` ` `// This code is contributed by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

5

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.

## Recommended Posts:

- Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps
- Check if it is possible to reach a number by making jumps of two given length
- Paths requiring minimum number of jumps to reach end of array
- Minimum number of jumps to reach end
- Reach the numbers by making jumps of two given lengths
- Minimum steps to reach the Nth stair in jumps of perfect power of 2
- Count minimum factor jumps required to reach the end of an Array
- Find minimum moves to reach target on an infinite line
- Minimum moves to reach target on a infinite line | Set 2
- Find if two people ever meet after same number of jumps
- Slope of the line parallel to the line with the given slope
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Number of jumps for a thief to cross walls
- Climb n-th stair with all jumps from 1 to n allowed (Three Different Approaches)
- Check if it is possible to move from (a, 0) to (b, 0) with given jumps
- Source to destination in 2-D path with fixed sized jumps
- Path traversed using exactly M coins in K jumps
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum jumps required to make a group of persons sit together
- Check if a given value can be reached from another value in a Circular Queue by K-length jumps

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.