FastPrepMinimum Vector Read and Rotate Uses

Minimum Vector Read and Rotate Uses

IMC Trading logoIMC Trading● EasyNEW GRADOA
Learn

Problem statement

An unknown vector has length indexed entries. You can use either of two machines:

  • The read machine reveals the current first entry.
  • The rotation machine shifts entries 2 through n one place left and moves the first entry to the last position.

Find the minimum total number of machine uses needed to read every original indexed entry. For this exercise, assume each use costs one operation, a read does not modify the vector, each rotation persists, and an entry must actually be read even if its value equals another entry or could be guessed.

For this exercise, assume length is the supplied positive integer and the result is an exact signed 64-bit integer. Complete minimumMachineUses(length).

Function

minimumMachineUses(length: long) → long

Examples

Example 1

length = 1return = 1

One front read reveals the only entry; no rotation is needed.

Example 2

length = 3return = 5

Read, rotate, read, rotate, read reveals all three indexed entries using five machine operations.

Example 3

length = 5return = 9

Five reads and four rotations are sufficient. A rotation after the final read would add an unnecessary operation.

Constraints

  • For this exercise, assume 1 <= length <= 1000000000000.
  • Only one-position left rotations and front reads are allowed.
  • Every use of either machine counts toward the objective.

More IMC Trading problems

See IMC Trading hiring insights
public long minimumMachineUses(long length) {
    // Write your solution here.
}
length1
expected1
Checking account…