1
long long fun(long long a) { // sum of all natural from 1 to a2
long long b = a * (a + 1) / 2;8
int reachNumber(int target) {9
long long i = 1, j = pow(10, 5), x = abs(target),10
ans = 0; // for -ve or +ve positive of a number the minimum no. of11
// steps from the origin will be same13
while (i <= j) { // binary search to search if x is continous sum of some14
// natural number starting from 115
long long m = (i + j) / 2;27
if (ans != 0) { // If we found our ans return it31
// in this for loop i have set the limit too high it can be less then 10^632
// as max value j can be is 44723, so loop will never run fully, whatever33
// the value of j will be, loop will maximum run for 3-10 iterations34
for (int l = j + 1; l < 100000; l++) { // in the end of binary search we get the value of j(or35
// high end) as the position of the number(in the36
// sequence of continous sum of natural number from 1,37
// i.e. 1, 3,6,10........) whose value is just less38
// than x(searching element)40
if ((fun(l) - x) % 2 == 0) { // as the total step will be more than x if we go backward from41
// zero, thing to note is that if we go -ve direction we also42
// have to come back so we covering even distance43
ans = l; // when the first fun(l) - x is even that l is our minimum jump45
break; // no need to search further