Google | Phone | US
Anonymous User
2159

Given two numbers a, b. Print all possible outputs and their paths where each path contains a unique difference of any two numbers in the set.

Ex.

Possible path given a = 7, b = 2:
[7, 2]
[7, 2, 5]
[7, 2, 5, 3]
[7, 2, 5, 3, 4]
[7, 2, 5, 3, 4, 1]
[7, 2, 5, 3, 4, 1, 6]

print all possible outputs:
[[7, 2], [7, 2, 5], [7, 2, 5, 3], [7, 2, 5, 3, 4], [7, 2, 5, 3, 4, 1], [7, 2, 5, 3, 4, 1, 6]]
[[7, 2], [7, 2, 5], [7, 2, 5, 3], [7, 2, 5, 3, 1], [7, 2, 5, 3, 1, 4], [7, 2, 5, 3, 1, 4, 6]]
[[7, 2], [7, 2, 5], [7, 2, 5, 3], [7, 2, 5, 3, 1], [7, 2, 5, 3, 1, 6], [7, 2, 5, 3, 1, 6, 4]]

My solution involved a queue where we iteratively append to queue for each unique diff we encounter for each step and print path of output when we can't add anymore diffs.

Comments (9)