Find the number of jumps to reach X in the number line from zero

09 Jun, 2022
Given an integer 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 = 8
Output : 4
Explanation : 
0 -> -1 -> 1 -> 4-> 8 are possible stages.

Input : X = 9
Output : 5
Explanation : 
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.
In any of these n jumps, if instead of jumping right, you jumped left in the kth jump (k<=n), 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++
Java
Python3
C#
PHP
Javascript
// C++ program to find the number of jumps// to reach X in the number line from zero #include <bits/stdc++.h>using namespace std; // Utility function to calculate sum// of numbers from 1 to xint getsum(int x){    return (x * (x + 1)) / 2;} // Function to find the number of jumps// to reach X in the number line from zeroint 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 codeint main(){    int n = 9;     cout << countJumps(n);     return 0;}
// Java program to find the number of jumps// to reach X in the number line from zero class GFG{     // Utility function to calculate sum// of numbers from 1 to xstatic int getsum(int x){    return (x * (x + 1)) / 2;} // Function to find the number of jumps// to reach X in the number line from zerostatic 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 codepublic static void main(String args[]){    int n = 9;     System.out.println(countJumps(n));}} // This code is contributed by Ryuga
# Python 3 program to find the number of jumps# to reach X in the number line from zero # Utility function to calculate sum# of numbers from 1 to xdef getsum(x):    return int((x * (x + 1)) / 2) # Function to find the number of jumps# to reach X in the number line from zerodef 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 codeif __name__ == '__main__':    n = 9     print(countJumps(n)) # This code is contributed by# Surendra_Gangwar
// C# program to find the number of jumps// to reach X in the number line from zerousing System; class GFG{     // Utility function to calculate sum// of numbers from 1 to xstatic int getsum(int x){    return (x * (x + 1)) / 2;} // Function to find the number of jumps// to reach X in the number line from zerostatic 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 codestatic void Main(){    int n = 9;     Console.WriteLine(countJumps(n));}} // This code is contributed by mits
<?php// PHP program to find the number of jumps// to reach X in the number line from zero // Utility function to calculate sum// of numbers from 1 to xfunction getsum($x){    return ($x * ($x + 1)) / 2;} // Function to find the number of jumps// to reach X in the number line from zerofunction 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?>
<script> // Javascript program to find the number of jumps// to reach X in the number line from zero // Utility function to calculate sum// of numbers from 1 to xfunction getsum(x){    return (x * (x + 1)) / 2;}   // Function to find the number of jumps// to reach X in the number line from zerofunction countJumps(n){    // First make number positive    // Answer will be same either it is    // Positive or negative    n = Math.abs(n);       // To store required answer    let 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       let n = 9;       document.write(countJumps(n));           </script>
5
 
Time Complexity: O(n)
Auxiliary Space: O(1)
Mithun Kumar
ankthon
SURENDRA_GANGWAR
Akanksha_Rai
ManasChhabra2
target_2
gabaa406
prashantkumar dhotre
sachinsom507
subhamkumarm348
Constructive Algorithms
Numbers
Greedy
Mathematical
Greedy
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Fractional Knapsack Problem
Job Sequencing Problem
Minimum Number of Platforms Required for a Railway/Bus Station
Policemen catch thieves
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
C++ Data Types
Merge two sorted arrays
Operators in C / C++