FastPrepMinimum Last-Element Relocations

Minimum Last-Element Relocations

Chubb logoChubb● MediumNEW GRADOA
Learn

Problem statement

The arrays current and desired contain the same multiset of values. In one operation, remove the last element of the current array and insert it at any position.

Return the minimum number of operations needed to transform current into desired.

Function

minimumRelocations(current: int[], desired: int[]) → int

Examples

Example 1

current = [2,1,3,5,4]desired = [2,4,1,5,3]return = 2

The unmatched suffix is exactly the set of elements that must be extracted and reinserted.

Example 2

current = [1,2,3]desired = [1,2,3]return = 0

The unmatched suffix is exactly the set of elements that must be extracted and reinserted.

Example 3

current = [1,2,3]desired = [3,2,1]return = 2

The unmatched suffix is exactly the set of elements that must be extracted and reinserted.

Constraints

  • 1 <= current.length == desired.length <= 200000.
  • The arrays contain the same multiset.

Source note: Author-written prompt excerpt retained for source fidelity.

More Chubb problems

See Chubb hiring insights
public int minimumRelocations(int[] current, int[] desired) {
    // Write your code here.
}
current[2,1,3,5,4]
desired[2,4,1,5,3]
expected2
Checking account…