Transformation: from A to B

You are given two positive integers a and b. You want to transform a into b using exactly two allowed operations:

  • Multiply the current number by 2 (i.e., x = 2 * x).
  • Append the digit 1 to the right of the current number (i.e., x = 10 * x + 1).

Find a valid sequence of operations to achieve this. Return an array containing the sequence of numbers starting from a and ending at b. If it is impossible to transform a into b, return an empty array.

Example 1:

Input: a = 2, b = 162

Output: [2, 4, 8, 81, 162]

Explanation:

2 * 2 = 4, 4 * 2 = 8, 8 * 10 + 1 = 81, 81 * 2 = 162.

Example 2:

Input: a = 4, b = 42

Output: [ ]

Explanation:

There is no valid sequence of operations that can turn 4 into 42.

Now Your Turn!

Pick the correct output for the given input

Input: a = 10, b = 201

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

1 <= a < b <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
vector<int> transform(int a, int b) {
}
};
Test Case

Input:

A
B