Maximum difference Indexes
Easy Accuracy: 65.29% Submissions: 403 Points: 2

Given an array A[]of size N. Consider differences between first and last occurrences of all elements and find the maximum such difference.  Note that if any element appears only once, then the difference for the element is 0.
 

Example 1:

Input:
N = 9
A[] = {2, 1, 3, 4, 2, 1, 5, 1, 7}
Output:
6
Explanation:
For the above test case: Element 1,
index 1 and 7. Maximum difference 6.

 

Your Task:  
You don't need to read input or print anything. Your task is to complete the function leftIndex() which takes the array A[] and its size N as inputs and returns the Maximum Difference.

 

Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(N)

 

Constraints:
1<=N<=105
-105<=Ai<=105

 

 

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.


to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Maximum difference Indexes

Output Window