Problem Statement
You are given two ints: a and b. You are now going to perform a sequence of zero or more steps. In each step you will double one of the integers and increment the other. Your goal is to reach a state in which both integers have the same value.
More precisely, in each step you will perform one of the following two moves:
Move '0': add 1 to a, multiply b by 2
Move '1': multiply a by 2, add 1 to b
Any sequence of steps can now be written as a string of zeros and ones. It is guaranteed that for each pair a, b that satifies the constraints (given below) it is possible to reach the goal in at most 2500 steps. Find any such sequence of steps and return a String containing its description.
Note that your solution does not have to minimize the number of steps. Any valid solution that consists of at most 2500 steps will be accepted.
Definition
Class: DoubleOrOne
Method: findSequence
Parameters: int, int
Returns: String
Method signature: String findSequence(int a, int b)
(be sure your method is public)
Notes
- The fact that a and b are given as ints does not imply any upper bound on their values. For example, if you start with a=1 and perform move '1' exactly 1000 times, you will have a=2^1000.
Constraints
- a,b will be between 0 and 100, inclusive.
Examples
0)
1
1
Returns: ""
Nothing needs to be done here.
1)
1
2
Returns: "11"
One way to make a,b equal is to apply move '1' twice. In this case, we get the following sequence: (1,2) -> (2,3) -> (4,4).
2)
2
1
Returns: "1110100"
This example is the same as Example 1, only with a and b swapped. Here we intentionally show a different correct sequence of steps. Note that the answer "00" would also be accepted.
3)
10
24
Returns: "001101011"
(10, 24) -> (11, 48) -> (12, 96) -> (24, 97) -> (48, 98) -> (49, 196) -> (98, 197) -> (99, 394) -> (198, 395) -> (396, 396)
4)
0
64
Returns: "10111101111"
5)
67
25
Returns: "11111101111100000001101000000"