FastPrepIndex of the Maximum After Left Rotations

Index of the Maximum After Left Rotations

Chubb logoChubb● MediumNEW GRADOA
Learn

Problem statement

You are given a nonempty integer array arr and an array of nonnegative rotation queries rotate.

Each query is independent. For every rotate[q], start from the original array, rotate it left by that many positions, and return the zero-based index of a maximum element. If the maximum occurs more than once after rotation, return its smallest index. Return one index per query.

Function

maximumIndicesAfterRotations(arr: int[], rotate: int[]) → int[]

Examples

Example 1

arr = [1,5,3]rotate = [0,1,4]return = [1,0,0]

The maximum starts at index 1. One left rotation moves it to index 0; four rotations have the same effect as one.

Example 2

arr = [5,1,5,2]rotate = [0,1,2,3]return = [0,1,0,1]

For each independent rotation, the smaller rotated index among the two maximum values is returned.

Constraints

  • 1 <= arr.length, rotate.length <= 200000.
  • -1000000000 <= arr[i] <= 1000000000.
  • 0 <= rotate[q] <= 1000000000.

More Chubb problems

See Chubb hiring insights
public int[] maximumIndicesAfterRotations(int[] arr, int[] rotate) {
    // Write your solution here.
}
arr[1,5,3]
rotate[0,1,4]
expected[1,0,0]
Checking account…