Problem: Ninja's Robot
The Ninja has a robot which navigates an infinite number line starting at position 0 with an initial speed of +1. The robot follows a set of instructions which includes ‘A’ (Accelerate) and ‘R’ (Reverse) to alter its movement.
The robot reacts as follows:
- If instructed with “A”, the robot updates:
position += speed
andspeed *= 2
. - If instructed with “R”, the robot changes direction: if
speed
is positive thenspeed = -1
, otherwisespeed = +1
. Theposition
remains identical.
Task:
For a given positive integer ‘TARGET’, determine and return the minimum length of the instruction sequence required for the robot to reach ‘TARGET’.
Example:
Input:
T = 2
TARGET = 3
TARGET = 6
Output:
2
5
Explanation:
- For the first test case with TARGET = 3, a possible shortest sequence is “AA” which leads the robot to position 3.
- For the second test case with TARGET = 6, a possible shortest sequence is “AAAAA” which leads the robot to position 6.
Constraints:
1 ≤ T ≤ 50
1 ≤ TARGET ≤ 10000
Note that the robot is capable of reaching negative positions as well.
Be the first one to answer
Add answer anonymously...
Top LimeRoad Full Stack Developer interview questions & answers
Popular interview questions of Full Stack Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app