P16212 [GeeksforGeeks] Minimum number of steps to reach a given number5234
普及
题目描述

Given an infinite number line. You start at 0 and can go either to the left or to the right. The condition is that in the i^{th} move, youmust take i steps. Given a destination D , find the minimum number of steps required to reach that destination.
Example 1:
Input: D = 2
Output: 3
Explaination: The steps takn are +1, -2 and +3.
Example 2:
Input: D = 10
Output: 4
Explaination: The steps are +1, +2, +3 and +4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSteps() which takes the value D as input parameter and returns the minimum number of steps required to reach the dedstination D from 0.
Expected Time Complexity: O(D)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ D ≤ 10000

输入格式

见题面

输出格式

见题面

样例输入
D = 2
样例输出
3