05. Maximum Index
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
Initialize the answer (ans) variable to -1, representing the maximum difference of indices.
Create two arrays (lmin and rmax) to store the minimum values on the left and maximum values on the right for each element.
Calculate the minimum values on the left (lmin) using a loop and store them in the array.
Calculate the maximum values on the right (rmax) using a loop and store them in the array.
Initialize two pointers (i and j) to iterate through the arrays.
While both pointers are within the array bounds:
Compare the minimum on the left (lmin[i]) and maximum on the right (rmax[j]).
If lmin[i] is less than rmax[j], update the answer (ans) with the maximum difference of indices (j - i), and increment j.
Otherwise, increment i.
Return the final answer.
Time Complexity: O(N)
, where N is the size of the array.
Auxiliary Space Complexity: O(N)
, two additional arrays of size N (lmin and rmax) are used.
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.