# Maximum difference between first and last indexes of an element in array

Given an array of n integers. The task is to find the difference of first and last index of each distinct element so as to maximize the difference.

Examples:

Input : {2, 1, 3, 4, 2, 1, 5, 1, 7} Output : 6 Element1has itsfirst index= 1 andlast index= 7Difference= 7 - 1 = 6 Other elements have a smaller first and last index difference Input : {2, 2, 1, 1, 8, 8, 3, 5, 3} Output : 2 Maximum difference is for indexes of element 3.

A **simple approach** is to run two loops and find the difference for each element and accordingly update the **max_diff**. It has a time complexity of O(n^{2}) and the approach also needs to keep track of the elements that have been visited so that difference for them is not calculated unnecessarily.

An **efficient approach** uses hashing. It has the following steps.

- Traverse the input array from left to right.
- For each distinct element map its first and last index in the hash table.
- Traverse the hash table and calculate the first and last index difference for each element.
- Accordingly update the
**max_diff**.

In the following implementation unordered_map has been used for hashing as the range of integers is not known.

## C++

`// C++ implementation to find the maximum difference ` `// of first and last index of array elements ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// function to find the ` `// maximum difference ` `int` `maxDifference(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// structure to store first and last ` ` ` `// index of each distinct element ` ` ` `struct` `index ` ` ` `{ ` ` ` `int` `f, l; ` ` ` `}; ` ` ` ` ` `// maps each element to its ` ` ` `// 'index' structure ` ` ` `unordered_map<` `int` `, index> um; ` ` ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `{ ` ` ` `// storing first index ` ` ` `if` `(um.find(arr[i]) == um.end()) ` ` ` `um[arr[i]].f = i; ` ` ` ` ` `// storing last index ` ` ` `um[arr[i]].l = i; ` ` ` `} ` ` ` ` ` `int` `diff, max_diff = INT_MIN; ` ` ` ` ` `unordered_map<` `int` `, index>::iterator itr; ` ` ` ` ` `// traversing 'um' ` ` ` `for` `(itr=um.begin(); itr != um.end(); itr++) ` ` ` `{ ` ` ` `// difference of last and first index ` ` ` `// of each element ` ` ` `diff = (itr->second).l - (itr->second).f; ` ` ` ` ` `// update 'max_dff' ` ` ` `if` `(max_diff < diff) ` ` ` `max_diff = diff; ` ` ` `} ` ` ` ` ` `// required maximum difference ` ` ` `return` `max_diff; ` `} ` ` ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `int` `arr[] = {2, 1, 3, 4, 2, 1, 5, 1, 7}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << ` `"Maximum Difference = "` ` ` `<<maxDifference(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the maximum difference ` `// of first and last index of array elements ` `import` `java.util.HashMap; ` `import` `java.util.Map; ` ` ` `public` `class` `MaxDiffIndexHashing { ` ` ` ` ` `static` `class` `Element { ` ` ` `int` `first; ` ` ` `int` `second; ` ` ` ` ` `public` `Element() { ` ` ` `super` `(); ` ` ` `} ` ` ` ` ` `public` `Element(` `int` `first, ` `int` `second) { ` ` ` `super` `(); ` ` ` `this` `.first = first; ` ` ` `this` `.second = second; ` ` ` `} ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` ` ` `int` `arr[]={` `2` `, ` `1` `, ` `3` `, ` `4` `, ` `2` `, ` `1` `, ` `5` `, ` `1` `, ` `7` `}; ` ` ` `System.out.println(` `"Maximum Difference= "` `+ maxDiffIndices(arr)); ` ` ` `} ` ` ` ` ` `private` `static` `int` `maxDiffIndices(` `int` `[] arr) { ` ` ` `int` `n = arr.length; ` ` ` `int` `maxDiffIndex = ` `0` `; ` ` ` `Map<Integer, Element> map = ` `new` `HashMap<Integer, Element>(); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `if` `(map.containsKey(arr[i])) { ` ` ` `Element e = map.get(arr[i]); ` ` ` `e.second = i; ` ` ` `} ` `else` `{ ` ` ` `Element e = ` `new` `Element(); ` ` ` `e.first = i; ` ` ` `map.put(arr[i], e); ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `for` `(Map.Entry<Integer, Element> entry : map.entrySet()) { ` ` ` `Element e = entry.getValue(); ` ` ` `if` `((e.second - e.first) > maxDiffIndex) ` ` ` `maxDiffIndex = e.second - e.first; ` ` ` `} ` ` ` ` ` `return` `maxDiffIndex; ` ` ` `} ` ` ` `} ` |

*chevron_right*

*filter_none*

Output:

Maximum Difference = 6

Time Complexity: O(n)

This article is contributed by **Ayush Jauhari**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Maximum element in an array such that its previous and next element product is maximum
- Replace each element by the difference of the total size of the array and frequency of that element
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Maximum difference between two elements such that larger element appears after the smaller number
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Maximum product of indexes of next greater on left and right
- Sum of minimum absolute difference of each array element
- Search an element in an array where difference between adjacent elements is 1
- Minimize sum of adjacent difference with removal of one element from array
- Maximum absolute difference in an array
- Maximum possible difference of two subsets of an array
- Maximum sum of absolute difference of an array
- Python Set difference to find lost element from a duplicated array
- Find the sum of maximum difference possible from all subset of a given array.
- Find the maximum element in the array other than A
_{i}