Maximum Index | Practice | GeeksforGeeks

Given an array A[] of N positive integers. The task is to find the maximum of j - i subjected to the constraint of A[i] < A[j] and i < j.
 
Example 1:
Input:
N = 2
A[] = {1, 10}
Output:
1
Explanation:
A[0]<A[1] so (j-i) is 1-0 = 1.
Example 2:
Input:
N = 9
A[] = {34, 8, 10, 3, 2, 80, 30, 33, 1}
Output:
6
Explanation:
In the given array A[1] < A[7]
satisfying the required 
condition(A[i] < A[j]) thus giving 
the maximum difference of j - i 
which is 6(7-1).

 
Your Task:
The task is to complete the function maxIndexDiff() which finds and returns maximum index difference. Printing the output will be handled by driver code. Return -1 in case no such index is found.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 107
0 ≤ A[i] ≤ 109
 
0
visveswar190126110 hours ago
int maxIndexDiff(int A[], int n)    {        // Your code here      int max_difference=0;      int diff=0;      for(int i=0;i<n;i++ )      {         for(int j=n-1;j>=i;j--)         {             if(A[i]<=A[j])             {                 diff=j-i;                 max_difference=max(max_difference,diff);                 break;             }         }      }           return max_difference;         }
0
aniket6518gadhe6 days ago
JAVA SOLUTION
Total Time Taken:
0.56/1.98
 
    static int maxIndexDiff(int A[], int N) {                 int index = 0; int max_diff = 0;            for(int i = 0; i < N; i++){          int j = N-1;          while(j>i){              if(A[i]<=A[j]){                  int diff = j - i;                  max_diff = Math.max(diff, max_diff);                  break;              }              j--;          }      }      return max_diff;   }
0
atharvsharma08086 days ago
 int max=0;       for(int i=0;i<N;i++)       {           int count=0;           for(int j=N-1;j>=0;j--)           {               if(A[i]<=A[j])               {                   count=j-i;                   break;               }           }           if(count>max)           {               max=count;           }       }       return max;
+2
lawbindpandey01w1 week ago
Time complexity : O(N) and Aux space : O(N)
 
 
int maxIndexDiff(int A[], int N)    {        // Your code here       vector<int> minA(N);       vector<int> maxA(N);              int curr_min = A[0];       int curr_max = A[N - 1];              minA[0] = 0;       maxA[N - 1] = N - 1;                     for(int i = 1 ; i < N; i++){                      if(A[i] < curr_min){               minA[i] = i;               curr_min = A[i];           }                      else               minA[i] = minA[i - 1];                  }
       //for(auto x : minA) cout << x << " "; cout << "\n";              for(int i = N - 2; i >= 0; i--){                      if(A[i] > curr_max){               maxA[i] = i;               curr_max = A[i];           }                          else               maxA[i] = maxA[i + 1];                  }       //for(auto x :  maxA) cout << x << " "; cout << "\n";              int max_diff = 0;       int j = 0;       for(int i = 0; i < N && j < N; i++){                                 while(j < N &&  A[minA[i]] <= A[maxA[j]] && i <= j){                              max_diff = max(max_diff, j - i);               j++;           }                             }                     return max_diff;          }
 
 
 
0
amrit_kumar2 weeks ago
int maxIndexDiff(int A[], int N) 
    { 
        int maxDiff = INT_MIN;
        int right[N];
        int left[N];
        for(int i=N-1;i>=0;i--)
        {
            if(i==N-1) right[i] = A[i];
            else right[i] = max(right[i+1],A[i]);
            
        }
        int i=0,j=0;
        while(i<N&&j<N)
        {
            while(j<N &&A[i]<=right[j]) j++;
            maxDiff = max(maxDiff, j-i-1);
            i++; 
            
        }
        return maxDiff;
        
        
    }
+1
ravikant021112 weeks ago
 // simplest java solution       int index = 0; int max_diff = -1;              for(int i = 0; i < N; i++){           int j = index + 1;           while(j<N){               if(arr[i] <= arr[j]){                   int diff = j - i;                   max_diff = Math.max(diff, max_diff);                   index = j;               }               j++;           }       }              return max_diff;
+1
visveswar19012612 weeks ago
 int maxIndexDiff(int A[], int n)    {        // Your code here     int max_diff=0;     int i=0;     while(i<n)     {                  for(int j=0;j<n;j++)         {             if(A[i]<=A[j])             {                 max_diff=max(max_diff,j-i);             }         }         i++;     }     return max_diff;             }};
-2
parmarabhishek0622 weeks ago
C++ EASY SOLUTION(0.27 SEC)
int maxIndexDiff(int A[], int n)    {        int diff = 0;       int max = 0;       for(int i = 0;i<n;i++){           for(int j = n-1;j>i;j--){               if(A[i]<=A[j]){                   diff = j-i;                   break;               }           }           if(diff>max){                   max = diff;               }       }       return max;       if(max==0){           return {-1};       }   }
 
+2
arpita biswal3 weeks ago
class Solution {   // Function to find the maximum index difference.   maxIndexDiff(A, N)   {       //your code here       let i=0,j=N-1,maxdiff=0;       while(i<=j){           if(A[i]<=A[j]){               maxdiff=Math.max(maxdiff,j-i);               j=N-1;               i++;           }           else{               j--;           }       }       return maxdiff;   }}
0
charansaisadla3 weeks ago
WHAT is WRONG
def maxIndexDiff(self,A, N):        ##Your code here       dic={}       for i in range(N):           dic[A[i]]=i       A.sort()       max1=0       for i in range(N-1):
               diff=dic[A[i+1]]-dic[A[i]]               max1=max(max1,diff)       return max1
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.