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 index1. Setindices[0] = 1.a = [1, 2, 3],rotate[1] = 2->a = [3, 1, 2]andindices[1] = 0.a = [1, 2, 3],rotate[2] = 3->a = [1, 2, 3]andindices[2] = 2.a = [1, 2, 3],rotate[3] = 4->a = [2, 3, 1]andindices[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 ofnintegers to rotateint rotate[m]: the rotations to perform
Returns
int[m]: each elementiis the index of the maximal element inaafterrotate[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).