Given a positive coordinate ‘X’ and you are at coordinate ‘0’, the task is to find the minimum time required to get to coordinate ‘X’ with the following move :

At time ‘t’, you can either stay at the same position or take a jump of length exactly ‘t’ either to the left or to the right. In other words, you can be at coordinate ‘x – t’, ‘x’ or ‘x + t’ at time ‘t’ where ‘x’ is the current position.

**Examples:**

Input:6Output:3 At time 1, jump from x = 0 to x = 1 (x = x + 1) At time 2, jump from x = 1 to x = 3 (x = x + 2) At time 3, jump from x = 3 to x = 6 (x = x + 3) So, minimum required time is 3.Input:9Output:4 At time 1, do not jump i.e x = 0 At time 2, jump from x = 0 to x = 2 (x = x + 2) At time 3, jump from x = 2 to x = 5 (x = x + 3) At time 4, jump from x = 5 to x = 9 (x = x + 4) So, minimum required time is 4.

**Approach:** The following greedy strategy works:

We just find the minimum ‘t’ such that `1 + 2 + 3 + ... + t >= X`

.

- If
`(t * (t + 1)) / 2 = X`

then answer is ‘t’. - Else if
`(t * (t + 1)) / 2 > X`

, then we find`(t * (t + 1)) / 2 – X`

and remove this number from the sequence`[1, 2, 3, ..., t]`

. The resulting sequence sums up to ‘X’.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` ` ` `#include <iostream> ` ` ` `using` `namespace` `std; ` ` ` ` ` `// returns the minimum time ` ` ` `// required to reach 'X' ` ` ` `long` `cal_minimum_time(` `long` `X) ` ` ` `{ ` ` ` ` ` `// Stores the minimum time ` ` ` `long` `t = 0; ` ` ` `long` `sum = 0; ` ` ` ` ` `while` `(sum < X) { ` ` ` ` ` `// increment 't' by 1 ` ` ` `t++; ` ` ` ` ` `// update the sum ` ` ` `sum = sum + t; ` ` ` `} ` ` ` ` ` `return` `t; ` ` ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `long` `n = 6; ` ` ` `long` `ans = cal_minimum_time(n); ` ` ` `cout << ` `"The minimum time required is : "` `<< ans ; ` ` ` ` ` `return` `0; ` ` ` ` ` `// This code is contributed by ANKITRAI1 ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG { ` ` ` ` ` `// returns the minimum time ` ` ` `// required to reach 'X' ` ` ` `static` `long` `cal_minimum_time(` `long` `X) ` ` ` `{ ` ` ` ` ` `// Stores the minimum time ` ` ` `long` `t = ` `0` `; ` ` ` `long` `sum = ` `0` `; ` ` ` ` ` `while` `(sum < X) { ` ` ` ` ` `// increment 't' by 1 ` ` ` `t++; ` ` ` ` ` `// update the sum ` ` ` `sum = sum + t; ` ` ` `} ` ` ` ` ` `return` `t; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `long` `n = ` `6` `; ` ` ` `long` `ans = cal_minimum_time(n); ` ` ` `System.out.println(` `"The minimum time required is : "` `+ ans); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the ` `# above approach ` ` ` `# returns the minimum time ` `# required to reach 'X' ` `def` `cal_minimum_time(X): ` ` ` ` ` `# Stores the minimum time ` ` ` `t ` `=` `0` ` ` `sum` `=` `0` ` ` ` ` `while` `(` `sum` `< X): ` ` ` ` ` `# increment 't' by 1 ` ` ` `t ` `=` `t ` `+` `1` ` ` ` ` `# update the sum ` ` ` `sum` `=` `sum` `+` `t; ` ` ` ` ` `return` `t; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `6` ` ` `ans ` `=` `cal_minimum_time(n) ` ` ` `print` `(` `"The minimum time required is :"` `, ans) ` ` ` `# This code is contributed By ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` ` ` `// returns the minimum time ` ` ` `// required to reach 'X' ` ` ` `static` `long` `cal_minimum_time(` `long` `X) ` ` ` `{ ` ` ` ` ` `// Stores the minimum time ` ` ` `long` `t = 0; ` ` ` `long` `sum = 0; ` ` ` ` ` `while` `(sum < X) { ` ` ` ` ` `// increment 't' by 1 ` ` ` `t++; ` ` ` ` ` `// update the sum ` ` ` `sum = sum + t; ` ` ` `} ` ` ` ` ` `return` `t; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main (){ ` ` ` `long` `n = 6; ` ` ` `long` `ans = cal_minimum_time(n); ` ` ` `Console.WriteLine(` `"The minimum time required is : "` `+ ans); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the ` `// above approach ` ` ` `// returns the minimum time ` `// required to reach 'X' ` `function` `cal_minimum_time(` `$X` `) ` `{ ` ` ` `// Stores the minimum time ` ` ` `$t` `= 0; ` ` ` `$sum` `= 0; ` ` ` ` ` `while` `(` `$sum` `< ` `$X` `) ` ` ` `{ ` ` ` ` ` `// increment 't' by 1 ` ` ` `$t` `++; ` ` ` ` ` `// update the sum ` ` ` `$sum` `= ` `$sum` `+ ` `$t` `; ` ` ` `} ` ` ` ` ` `return` `$t` `; ` `} ` ` ` `// Driver code ` `$n` `= 6; ` `$ans` `= cal_minimum_time(` `$n` `); ` `echo` `"The minimum time required is : "` `. ` `$ans` `; ` ` ` `// This code is contributed ` `// by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

The minimum time required is : 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.

## Recommended Posts:

- Minimum number of moves to reach N starting from (1, 1)
- Minimum moves to reach target on a infinite line | Set 2
- Find minimum moves to reach target on an infinite line
- Minimum number of moves required to reach the destination by the king in a chess board
- Minimum cost to reach a point N from 0 with two different operations allowed
- Probability of reaching a point with 2 or 3 steps at a time
- Minimum time required to cover a Binary Array
- Minimum time required to fill a cistern using N pipes
- Minimum time required to complete a work by N persons together
- Find minimum time to finish all jobs with given constraints
- Minimum minutes needed to make the time palindromic
- Minimum time to return array to its original state after given modifications
- Total position where king can reach on a chessboard in exactly M moves
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Expected number of moves to reach the end of a board | Dynamic programming
- Expected number of moves to reach the end of a board | Matrix Exponentiation
- Minimum decrement operations to make Array elements equal by only decreasing K each time
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Minimum number of given moves required to make N divisible by 25
- Find minimum moves to bring all elements in one cell of a matrix

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.