FastPrepMinimum Operations with Multiply by Two and Divide by Three

Minimum Operations with Multiply by Two and Divide by Three

Bloomberg LP logoBloomberg LP● HardNEW GRADONSITE INTERVIEW
Learn

Problem statement

Start at integer 1. Operation * replaces x with 2x; operation / replaces x with floor(x/3). Every intermediate state must remain between 1 and maxState.

Return a shortest operation string reaching target. Break shortest ties lexicographically with * before /. Return IMPOSSIBLE when unreachable.

Function

shortestOperationSequence(target: int, maxState: int) → String

Examples

Example 1

target = 10maxState = 16return = "****/*"

1->2->4->8->16->5->10.

Example 2

target = 3maxState = 16return = "****/*/"

The source's example reaches 3 through 16,5,10,3.

Constraints

  • 1 <= target <= maxState <= 10^6.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String shortestOperationSequence(int target, int maxState) {
  // Write your code here.
}
target10
maxState16
expected"****/*"
Checking account…