WhyRUHere's blog

By WhyRUHere, history, 5 weeks ago, In English

There is a man standing on origin. He can only move in x-axis.At time t-1 for next second he can either jump to x+t or x-t or x (x is the current position).we need to answer minimum time t at which the man reached the point n.

0<=n<=1e9

Test Cases

N=6 Answer: 3

Explanation: at t=0 he is at x=0 now when t=1 x=1; t=2,x=1+2=3; t=3,x=1+2+3=6;

so,t=3 is the answer.

N=2

Answer: 2

at t=0 for next second it decided not to jump so,t=1,x=0; at t=2,x=2;

so,2 is the answer.

If anyone having approach then please comment:))

Thanks__

 
 
 
 
  • Vote: I like it
  • -7
  • Vote: I do not like it

»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

While x<=n, add t to x(jump right). If x>n, at time x-n you should jump left(let Y be your pos at time x-n-1. Than you change it to Y — (x-n). So at end you will be at pos n.

»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it
Spoiler