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 and speed *= 2.
  • If instructed with “R”, the robot changes direction: if speed is positive then speed = -1, otherwise speed = +1. The position 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...
LimeRoad Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter