This updated information has been further expanded upon on my new website. You can find the updated details here: https://k5kc.com/cs/algorithms/finding-the-maximum-distance-between-increasing-elements-in-an-array/.
Problem
Given an array arr[], find the maximum j – i such that arr[j] > arr[i].Example
Input: {34, 8, 10, 3, 2, 80, 30, 33, 1} Output: 6 (j = 7, i = 1) Input: {9, 2, 3, 4, 5, 6, 7, 8, 18, 0} Output: 8 ( j = 8, i = 0) Input: {1, 2, 3, 4, 5, 6} Output: 5 (j = 5, i = 0) Input: {6, 5, 4, 3, 2, 1} Output: -1
Solution
Method 1 (Simple but Inefficient)Run two loops. In the outer loop, pick elements one by one from left. In the inner loop, compare the picked element with the elements starting from right side. Stop the inner loop when you see an element greater than the picked element and keep updating the maximum j-i so far.
code
int maxIndexDiff(int arr[], int n) { int maxDiff = -1; int i, j; for (i = 0; i < n; ++i) { for (j = n-1; j > i; --j) { if(arr[j] > arr[i] && maxDiff < (j - i)) maxDiff = j - i; } } return maxDiff; }
Time Complexity: O(n^2)
Method 2 (Efficient)
To solve this problem, we need to get two optimum indexes of arr[]: left index i and right index j. For an element arr[i], we do not need to consider arr[i] for left index if there is an element smaller than arr[i] on left side of arr[i]. Similarly, if there is a greater element on right side of arr[j] then we do not need to consider this j for right index.
So we construct two auxiliary arrays LMin[] and RMax[] such that LMin[i] holds the smallest element on left side of arr[i] including arr[i], and RMax[j] holds the greatest element on right side of arr[j] including arr[j]. After constructing these two auxiliary arrays, we traverse both of these arrays from left to right. While traversing LMin[] and RMa[] if we see that LMin[i] is greater than RMax[j], then we must move ahead in LMin[] (or do i++) because all elements on left of LMin[i] are greater than or equal to LMin[i]. Otherwise we must move ahead in RMax[j] to look for a greater j – i value.
Here is the java code
int maxIndexDiff(int arr[], int n) { int maxDiff; int i, j; int[] LMin = new int[n]; int[] RMax = new int[n]; // Construct LMin[] such that LMin[i] stores the minimum value // from (arr[0], arr[1], ... arr[i]) LMin[0] = arr[0]; for (i = 1; i < n; ++i) LMin[i] = Math.min(arr[i], LMin[i-1]); // Construct RMax[] such that RMax[j] stores the maximum value // from (arr[j], arr[j+1], ..arr[n-1]) RMax[n-1] = arr[n-1]; for (j = n-2; j >= 0; --j) RMax[j] = Math.max(arr[j], RMax[j+1]); // Traverse both arrays from left to right to find optimum j - i // This process is similar to merge() of MergeSort i = 0, j = 0, maxDiff = -1; while (j < n && i < n) { if (LMin[i] < RMax[j]) { maxDiff = max(maxDiff, j-i); j = j + 1; } else i = i+1; } return maxDiff; }
Time Complexity: O(n)
Auxiliary Space: O(n)
Examples of running method 2
Example 1
Input Array : [7, 3, 9, 2, 1, 11, 0] LMin : [7, 3, 3, 2, 1, 1 , 0] IndexOfLeftMinimum : [0, 1, 1, 3, 4, 4, 6] RMax : [11,11,11,11,11,11, 0] IndexOfRightMaximum : [5, 5, 5, 5, 5, 5, 6] Distance Array : [5, 4, 4, 3, 1, 1, 0] Maximum value in distance array = 5 Corresponding i (from IndexOfLeftMinimum array) = 0 Corresponding j (from IndexOfRightMaximum array) = 5 Solution: i=0, j=5
Example 2:
Input Array : [0, 1, 2, 3, 4] IndexOfLeftMinimum : [0, 0, 0, 0, 0] IndexOfRightMaximum : [4, 4, 4, 4, 4] Maximum value in distance array = 4 Corresponding i (from IndexOfLeftMinimum array) = 0 Corresponding j (from IndexOfRightMaximum array) = 4 Solution: i=0, j=4
Example 3:
Input Array : [4, 3, 2, 1, 0] IndexOfLeftMinimum : [0, 1, 2, 3, 4] IndexOfRightMaximum : [0, 1, 2, 3, 4] Maximum value in distance array = 0 Corresponding i (from IndexOfLeftMinimum array) = None Corresponding j (from IndexOfRightMaximum array) = None Solution: No such pair
Source - geeksforgeeks
Nice Example ... visit more Java examples
ReplyDeleteThanks Java Proficiency. But the problem that you have provided is not same as above problem. Regards.
Delete