03. Maximum Index
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
It creates two vectors v_min
and v_max
, each of size n
. These vectors will store the minimum and maximum values encountered so far from the left and right sides of the array, respectively.
The first element of v_min
is initialized as arr[0]
(the first element of the array), and the last element of v_max
is initialized as arr[n - 1]
(the last element of the array).
Two loops are used to populate the v_min
and v_max
vectors. Starting from the second element (i = 1
), the loop calculates the minimum value between the current element and the previous minimum value and stores it in v_min[i]
. Similarly, starting from the second-to-last element (n - i - 1
), the loop calculates the maximum value between the current element and the next maximum value and stores it in v_max[n - i - 1]
.
After populating the v_min
and v_max
vectors, the code initializes ans
(the maximum difference) to 0 and sets i
and j
to 0.
A while loop is used to iterate until either i
or j
reaches the end of the array. The loop checks if the current minimum value (v_min[i]
) is less than or equal to the current maximum value (v_max[j]
). If true, it updates the maximum difference (ans
) if the difference between j
and i
is greater than the current maximum difference. Then, it increments j
to consider the next element. Otherwise, it increments i
to consider the next element.
Finally, when the while loop finishes, the function returns the maximum difference (ans
).
Time Complexity : O(n)
as it performs iterations only up to n
.
Auxiliary Space Complexity : O(n)
because we uses two additional arrays, v_min
and v_max
, of the same size of n
.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.