Minimum steps to destination
Medium Accuracy: 56.22% Submissions: 2348 Points: 4

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 ith move, youmust take i steps. Given a destination, 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

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.


to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Minimum steps to destination

Output Window