Ninjas's Robot

Ninja has a robot that can move in an infinite number line. The robot starts at position 0, with speed = +1. The robot moves automatically according to the sequence of instructions “A” (Accelerate) and “R” (Reverse).

When robot get an instruction "A", the robot does the following: position += speed, speed *= 2.

When the robot gets an instruction “R”, the robot does the following: if its speed is positive then speed = -1, otherwise speed = +1. The position of the robot will remain the same.

For example, after commands "AAARA", the robot goes to positions 0->1->3->7->7->6, and robot speed goes to 1->2->4->8->-1->-2.

You are given a positive integer ‘TARGET’, and your task is to find and return the length of the shortest sequence of instruction to reach position equals ‘TARGET’.

Note :

The position of the robot can be negative.

Input Format :

The first line contains a single integer ‘T’ representing the number of test cases. The 'T' test cases follow.

The first line of each test case will contain a single integer ‘TARGET’.

Output Format :

For each test case, print an integer representing the length of the shortest sequence of instruction to reach position equals ‘TARGET’.

Output for every test case will be printed in a separate line.

Note :

You don’t need to print anything, It has already been taken care of. Just implement the given function.

Constraints :

1 <= 'T' <= 50
1 <= ‘TARGET’ <= 10000

Time limit: 1 sec
CodingNinjas
author
2y
Breadth-First Search

The basic idea is to use modified Breadth-First Search (BFS) here We can keep track of all the possible positions of the robot after ‘N’ instructions (N = 0, 1, 2, 3, 4, ...) and return the smallest ‘N’ such that the ‘TARGET’ position is reached

 

The steps are as follows:

  1. Initialize an integer variable ‘N’:= 0.
  2. Create a queue of list/array ‘STATE’  Each element in the queue has an array/list consisting of two elements i.e position and speed. We enqueue list/array [0, 1] in it.
  3. Run a loop till the queue ‘STATE’ is not empty and in each step do the following -:
    1. Let ‘M’ be the size of the queue ‘STATE’
    2. Run a loop where ‘i’ ranges from 0 to ‘M’ and do the following -:
      1. Let ‘POS’ be the 0th element of list/array at front of queue ‘STATE’ and  ‘SPEED’ be its 1st element.
      2. If ‘POS’ = ‘TARGET’ then return ‘N’.
      3. Dequeue the front element of queue ‘STATE’.
      4. Enqueue list/array [‘POS’+’SPEED’, 2*SPEED] and [‘POS’, -1 * SPEED/|SPEED|] where |X| is absolute value of ‘X’
    3. Increment ‘N’ by 1
Space Complexity: O(2^n)Explanation:

O(2^N), where 'N' is the maximum number of instructions.

 

The space used by the queue will be O(2^N).

Time Complexity: O(2^n)Explanation:

O(2^N), where 'N' is the maximum number of instructions.

 

In each step we have two choices ‘R’ or ‘A’, Thus overall there are 2^N choices for ‘N’ instructions. Thus overall complexity will be O(2^N),

CodingNinjas
author
2y
Dynamic Programming

Let ‘N’ be the number of bits in the binary representation of ‘TARGET’, Then if ‘TARGET’ = 2^N -1, then the shortest sequence will consist of ‘N’ “A”s,

Otherwise, there are two poss...read more

Help your peers!
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
Get AmbitionBox app

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