Given an infinite number line from -INFINITY to +INFINITY and we are on zero. We can move n steps either side at each n’th time.

**Approach 1 : Using Tree**

1st time; we can move only 1 step to both ways, means -1 1; 2nd time we can move 2 steps from -1 and 1; -1 : -3 (-1-2) 1(-1+2) 1 : -1 ( 1-2) 3(1+2) 3rd time we can move 3 steps either way from -3, 1, -1, 3 -3: -6(-3-3) 0(-3+3) 1: -2(1-3) 4(1+3) -1: -4(-1-3) 2(-1+3) 3: 0(0-3) 6(3+3) Find the minimum number of steps to reach a given number n.

Examples:

Input : n = 10 Output : 4 We can reach 10 in 4 steps, 1, 3, 6, 10 Input : n = 13 Output : 5 We can reach 10 in 4 steps, -1, 2, 5, 9, 14

This problem can be modeled as tree. We put initial point 0 at root, 1 and -1 as children of root. Next level contains values at distance 2 and so on.

0 / -1 1 / / 1 -3 -1 3 / / / /

The problem is now to find the closes node to root with value n. The idea is to do Level Order Traversal of tree to find the closest node. Note that using DFS for closest node is never a good idea (we may end up going down many unnecessary levels).

Below is C++ implementation of above idea.

`// C++ program to find a number in minimum steps ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define InF 99999 ` ` ` `// To represent data of a node in tree ` `struct` `number { ` ` ` `int` `no; ` ` ` `int` `level; ` ` ` `public` `: ` ` ` `number() {} ` ` ` `number(` `int` `n, ` `int` `l) ` ` ` `: no(n), level(l) ` ` ` `{ ` ` ` `} ` `}; ` ` ` `// Prints level of node n ` `void` `findnthnumber(` `int` `n) ` `{ ` ` ` `// Create a queue and insert root ` ` ` `queue<number> q; ` ` ` `struct` `number r(0, 1); ` ` ` `q.push(r); ` ` ` ` ` `// Do level order traversal ` ` ` `while` `(!q.empty()) { ` ` ` `// Remove a node from queue ` ` ` `struct` `number temp = q.front(); ` ` ` `q.pop(); ` ` ` ` ` `// To avoid infinite loop ` ` ` `if` `(temp.no >= InF || temp.no <= -InF) ` ` ` `break` `; ` ` ` ` ` `// Check if dequeued number is same as n ` ` ` `if` `(temp.no == n) { ` ` ` `cout << ` `"Found number n at level "` ` ` `<< temp.level - 1; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// Insert children of dequeued node to queue ` ` ` `q.push(number(temp.no + temp.level, temp.level + 1)); ` ` ` `q.push(number(temp.no - temp.level, temp.level + 1)); ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `findnthnumber(13); ` ` ` `return` `0; ` `} ` |

Output :

Found number n at level 5

The above solution is contributed by **Mu Ven**.

**Approach 2 : Using Vector**

The above solution uses binary tree for nth time instance i.e. -n and n. But as the level of tree increases this becomes inefficient. For values like abs(200) or more above program gives segmentation fault.

Below solution does not make a tree and takes complexity equal to exact number of steps required. Also the steps required are printed in the array which equals the exact sum required.

**Main Idea:**

- Distance between +n and -n is 2*n. So if you negate a number from +ve to -ve it will create difference of 2*n from previous sum.
- If a number lies between n(n+1)/2 and (n+1)(n+2)/2 for any n then we go to step (n+1)(n+2)/2 and try to decrease the sum to the difference using idea discussed above.
- If we go to n(n+1)/2 and then try to increase than it will ultimately lead you to same number of steps.

And since you cannot negate any number (as sum is already less than required sum) from n(n+1)/2 this proves that it takes minimum number of steps.

`// CPP program to Find a ` `// number in minimum steps ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `vector<` `int` `> find(` `int` `n) ` `{ ` ` ` `// Steps sequence ` ` ` `vector<` `int` `> ans; ` ` ` ` ` `// Current sum ` ` ` `int` `sum = 0; ` ` ` `int` `i; ` ` ` ` ` `// Sign of the number ` ` ` `int` `sign = (n >= 0 ? 1 : -1); ` ` ` `n = ` `abs` `(n); ` ` ` ` ` `// Basic steps required to get ` ` ` `// sum >= required value. ` ` ` `for` `(i = 1; sum < n; i++) { ` ` ` `ans.push_back(sign * i); ` ` ` `sum += i; ` ` ` `} ` ` ` ` ` `// If we have reached ahead to destination. ` ` ` `if` `(sum > sign * n) { ` ` ` `/*If the last step was an odd number, ` ` ` `then it has following mechanism for ` ` ` `negating a particular number and ` ` ` `decreasing the sum to required number ` ` ` `Also note that it may require ` ` ` `1 more step in order to reach the sum. */` ` ` `if` `(i % 2) { ` ` ` `sum -= n; ` ` ` `if` `(sum % 2) { ` ` ` `ans.push_back(sign * i); ` ` ` `sum += i++; ` ` ` `} ` ` ` `ans[(sum / 2) - 1] *= -1; ` ` ` `} ` ` ` `else` `{ ` ` ` `/* If the current time instance is even ` ` ` `and sum is odd than it takes ` ` ` `2 more steps and few ` ` ` `negations in previous elements ` ` ` `to reach there. */` ` ` `sum -= n; ` ` ` `if` `(sum % 2) { ` ` ` `sum--; ` ` ` `ans.push_back(sign * i); ` ` ` `ans.push_back(sign * -1 * (i + 1)); ` ` ` `} ` ` ` `ans[(sum / 2) - 1] *= -1; ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `int` `n = 20; ` ` ` `if` `(n == 0) ` ` ` `cout << ` ```
"Minimum number of Steps: 0
Step sequence:
0"
``` `; ` ` ` `else` `{ ` ` ` `vector<` `int` `> a = find(n); ` ` ` `cout << ` `"Minimum number of Steps: "` `<< a.size() << ` ```
"
Step sequence:
"
``` `; ` ` ` `for` `(` `int` `i : a) ` ` ` `cout << i << ` `" "` `; ` ` ` `} ` ` ` `return` `0; ` `} ` |

Output :

Minimum number of Steps: 7 Step sequence: 1 2 3 -4 5 6 7

If n is the sum that it is required and s is the minimum steps then:

n = (s+1)*(s+2)/2 + 1 (or +2)

Hence n = O(s*s)

Therefore s = O(sqrt(n))

Space Complexity : O(sqrt(n))

Time complexity : O(sqrt(n))

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## leave a comment

## 0 Comments