Point / VO Coding Interview OA: Get Max Element Indexes After Left Circular Rotations

17 Views
No Comments

Array a consists of distinct, positive integers and serves as a work array. Array rotate contains a list of integers that define operations on the work array. For each element of the rotate array, that number of left circular rotations is applied to the work array. In one left circular rotation, the element at index 0 is moved to the highest index of the array, and all the other elements shift left by 1 index.

Given an array of distinct positive integers, perform each rotation in the second array and determine the index of the highest value element within the array.

Example

a = [1, 2, 3]
rotate = [1, 2, 3, 4]

The resulting arrays are:

  • a = [1, 2, 3], rotate[0] = 1 -> a = [2, 3, 1] and the maximum element is at index 1. Set indices[0] = 1.
  • a = [1, 2, 3], rotate[1] = 2 -> a = [3, 1, 2] and indices[1] = 0.
  • a = [1, 2, 3], rotate[2] = 3 -> a = [1, 2, 3] and indices[2] = 2.
  • a = [1, 2, 3], rotate[3] = 4 -> a = [2, 3, 1] and indices[3] = 1.

Return the new array made of the indices of the maximal elements, indices = [1, 0, 2, 1].

Note: Each rotation is performed on the original array formation.

Function Description

Complete the function getMaxElementIndexes in the editor.

getMaxElementIndexes has the following parameter(s):

  • int a[n]: an array of n integers to rotate
  • int rotate[m]: the rotations to perform

Returns

  • int[m]: each element i is the index of the maximal element in a after rotate[i] rotations

This problem asks you to report the index of the maximum element after each left circular rotation count in rotate. Since all values in a are distinct, the maximum element is fixed in the original array; you only need to map its index after a rotation using modular arithmetic: new_index = (max_index – k % n + n) % n. The sample a = [1, 2, 3] and rotate = [1, 2, 3, 4] produces [1, 0, 2, 1]. The efficient solution avoids physically rotating the array for every query and runs in O(n + m).

END
 0