# Maximum distance between two unequal elements

Given an array arr[], the task is to find the maximum distance between two unequal elements of the given array.

Examples:

Input: arr[] = {3, 2, 1, 2, 1}
Output: 4
The maximum distance is between the first and the last element.

Input: arr[] = {3, 3, 1, 3, 3}
Output: 2

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive approach: Traverse the whole array for every single element and find the longest distance of element which is unequal.

Efficient approach: By using the fact that the pair of unequal elements must include either first or last element or both, calculate the longest distance between unequal element by traversing the array either by fixing the first element or fixing the last element.

Below is the implementation of the above approach:

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the maximum distance ` `// between two unequal elements ` `int` `maxDistance(``int` `arr[], ``int` `n) ` `{ ` `    ``// If first and last elements are unequal ` `    ``// they are maximum distance apart ` `    ``if` `(arr != arr[n - 1]) ` `        ``return` `(n - 1); ` ` `  `    ``int` `i = n - 1; ` ` `  `    ``// Fix first element as one of the elements ` `    ``// and start traversing from the right ` `    ``while` `(i > 0) { ` ` `  `        ``// Break for the first unequal element ` `        ``if` `(arr[i] != arr) ` `            ``break``; ` `        ``i--; ` `    ``} ` ` `  `    ``// To store the distance from the first element ` `    ``int` `distFirst = (i == 0) ? -1 : i; ` ` `  `    ``i = 0; ` ` `  `    ``// Fix last element as one of the elements ` `    ``// and start traversing from the left ` `    ``while` `(i < n - 1) { ` ` `  `        ``// Break for the first unequal element ` `        ``if` `(arr[i] != arr[n - 1]) ` `            ``break``; ` `        ``i++; ` `    ``} ` ` `  `    ``// To store the distance from the last element ` `    ``int` `distLast = (i == n - 1) ? -1 : (n - 1 - i); ` ` `  `    ``// Maximum possible distance ` `    ``int` `maxDist = max(distFirst, distLast); ` `    ``return` `maxDist; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 4, 4, 1, 2, 1, 4 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << maxDistance(arr, n); ` ` `  `    ``return` `0; ` `} `

 `// Java implementation of the approach ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to return the maximum distance ` `// between two unequal elements ` `static` `int` `maxDistance(``int` `arr[], ``int` `n) ` `{ ` `    ``// If first and last elements are unequal ` `    ``// they are maximum distance apart ` `    ``if` `(arr[``0``] != arr[n - ``1``]) ` `        ``return` `(n - ``1``); ` ` `  `    ``int` `i = n - ``1``; ` ` `  `    ``// Fix first element as one of the elements ` `    ``// and start traversing from the right ` `    ``while` `(i > ``0``)  ` `    ``{ ` ` `  `        ``// Break for the first unequal element ` `        ``if` `(arr[i] != arr[``0``]) ` `            ``break``; ` `        ``i--; ` `    ``} ` ` `  `    ``// To store the distance from the first element ` `    ``int` `distFirst = (i == ``0``) ? -``1` `: i; ` ` `  `    ``i = ``0``; ` ` `  `    ``// Fix last element as one of the elements ` `    ``// and start traversing from the left ` `    ``while` `(i < n - ``1``) ` `    ``{ ` ` `  `        ``// Break for the first unequal element ` `        ``if` `(arr[i] != arr[n - ``1``]) ` `            ``break``; ` `        ``i++; ` `    ``} ` ` `  `    ``// To store the distance from the last element ` `    ``int` `distLast = (i == n - ``1``) ? -``1` `: (n - ``1` `- i); ` ` `  `    ``// Maximum possible distance ` `    ``int` `maxDist = Math.max(distFirst, distLast); ` `    ``return` `maxDist; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `arr[] = { ``4``, ``4``, ``1``, ``2``, ``1``, ``4` `}; ` `    ``int` `n = arr.length; ` `    ``System.out.print(maxDistance(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by anuj_67.. `

 `# Python implementation of the approach ` ` `  `# Function to return the maximum distance ` `# between two unequal elements ` `def` `maxDistance(arr, n): ` `     `  `    ``# If first and last elements are unequal ` `    ``# they are maximum distance apart ` `    ``if` `(arr[``0``] !``=` `arr[n ``-` `1``]): ` `        ``return` `(n ``-` `1``); ` ` `  `    ``i ``=` `n ``-` `1``; ` ` `  `    ``# Fix first element as one of the elements ` `    ``# and start traversing from the right ` `    ``while` `(i > ``0``): ` ` `  `        ``# Break for the first unequal element ` `        ``if` `(arr[i] !``=` `arr[``0``]): ` `            ``break``; ` `        ``i``-``=``1``; ` ` `  `    ``# To store the distance from the first element ` `    ``distFirst ``=` `-``1` `if``(i ``=``=` `0``) ``else` `i; ` ` `  `    ``i ``=` `0``; ` ` `  `    ``# Fix last element as one of the elements ` `    ``# and start traversing from the left ` `    ``while` `(i < n ``-` `1``): ` ` `  `        ``# Break for the first unequal element ` `        ``if` `(arr[i] !``=` `arr[n ``-` `1``]): ` `            ``break``; ` `        ``i``+``=``1``; ` ` `  `    ``# To store the distance from the last element ` `    ``distLast ``=` `-``1` `if``(i ``=``=` `n ``-` `1``) ``else` `(n ``-` `1` `-` `i); ` ` `  `    ``# Maximum possible distance ` `    ``maxDist ``=` `max``(distFirst, distLast); ` `    ``return` `maxDist; ` ` `  `# Driver code ` `arr ``=` `[``4``, ``4``, ``1``, ``2``, ``1``, ``4``]; ` `n ``=` `len``(arr); ` `print``(maxDistance(arr, n)); ` ` `  `# This code has been contributed by 29AjayKumar `

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to return the maximum distance ` `// between two unequal elements ` `static` `int` `maxDistance(``int` `[]arr, ``int` `n) ` `{ ` `    ``// If first and last elements are unequal ` `    ``// they are maximum distance apart ` `    ``if` `(arr != arr[n - 1]) ` `        ``return` `(n - 1); ` ` `  `    ``int` `i = n - 1; ` ` `  `    ``// Fix first element as one of the elements ` `    ``// and start traversing from the right ` `    ``while` `(i > 0)  ` `    ``{ ` ` `  `        ``// Break for the first unequal element ` `        ``if` `(arr[i] != arr) ` `            ``break``; ` `        ``i--; ` `    ``} ` ` `  `    ``// To store the distance from the first element ` `    ``int` `distFirst = (i == 0) ? -1 : i; ` ` `  `    ``i = 0; ` ` `  `    ``// Fix last element as one of the elements ` `    ``// and start traversing from the left ` `    ``while` `(i < n - 1) ` `    ``{ ` ` `  `        ``// Break for the first unequal element ` `        ``if` `(arr[i] != arr[n - 1]) ` `            ``break``; ` `        ``i++; ` `    ``} ` ` `  `    ``// To store the distance from the last element ` `    ``int` `distLast = (i == n - 1) ? -1 : (n - 1 - i); ` ` `  `    ``// Maximum possible distance ` `    ``int` `maxDist = Math.Max(distFirst, distLast); ` `    ``return` `maxDist; ` `} ` ` `  `// Driver code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `[]arr = { 4, 4, 1, 2, 1, 4 }; ` `    ``int` `n = arr.Length; ` `    ``Console.WriteLine(maxDistance(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by Tushil.. `

Output:
```4
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m, jit_t, 29AjayKumar

Article Tags :
Practice Tags :