03. Maximum Index
The problem can be found at the following link: Question Link
My approach
It creates two vectors
v_minandv_max, each of sizen. 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_minis initialized asarr[0](the first element of the array), and the last element ofv_maxis initialized asarr[n - 1](the last element of the array).Two loops are used to populate the
v_minandv_maxvectors. 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 inv_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 inv_max[n - i - 1].After populating the
v_minandv_maxvectors, the code initializesans(the maximum difference) to 0 and setsiandjto 0.A while loop is used to iterate until either
iorjreaches 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 betweenjandiis greater than the current maximum difference. Then, it incrementsjto consider the next element. Otherwise, it incrementsito consider the next element.Finally, when the while loop finishes, the function returns the maximum difference (
ans).
Time and Auxiliary Space Complexity
Time Complexity :
O(n)as it performs iterations only up ton.Auxiliary Space Complexity :
O(n)because we uses two additional arrays,v_minandv_max, of the same size ofn.
Code (C++)
class Solution{
public:
int maxIndexDiff(int arr[], int n) {
vector<int> v_min(n), v_max(n);
v_min[0] = arr[0];
v_max[n - 1] = arr[n - 1];
for (int i = 1; i < n; i++) {
v_min[i] = min(v_min[i - 1], arr[i]);
v_max[n - i - 1] = max(v_max[n - i], arr[n - i - 1]);
}
int ans = 0, i = 0, j = 0;
while (i < n && j < n) {
if (v_min[i] <= v_max[j]) {
ans = max(ans, j - i);
j++;
} else i++;
}
return ans;
}
};
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. 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 getlost01/gfg-potd repository.
Last updated
Was this helpful?