Maximum distance between two unequal elements
Last Updated :
08 Mar, 2022
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
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++
#include <bits/stdc++.h>
using namespace std;
int maxDistance( int arr[], int n)
{
if (arr[0] != arr[n - 1])
return (n - 1);
int i = n - 1;
while (i > 0) {
if (arr[i] != arr[0])
break ;
i--;
}
int distFirst = (i == 0) ? -1 : i;
i = 0;
while (i < n - 1) {
if (arr[i] != arr[n - 1])
break ;
i++;
}
int distLast = (i == n - 1) ? -1 : (n - 1 - i);
int maxDist = max(distFirst, distLast);
return maxDist;
}
int main()
{
int arr[] = { 4, 4, 1, 2, 1, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxDistance(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int maxDistance( int arr[], int n)
{
if (arr[ 0 ] != arr[n - 1 ])
return (n - 1 );
int i = n - 1 ;
while (i > 0 )
{
if (arr[i] != arr[ 0 ])
break ;
i--;
}
int distFirst = (i == 0 ) ? - 1 : i;
i = 0 ;
while (i < n - 1 )
{
if (arr[i] != arr[n - 1 ])
break ;
i++;
}
int distLast = (i == n - 1 ) ? - 1 : (n - 1 - i);
int maxDist = Math.max(distFirst, distLast);
return maxDist;
}
public static void main (String[] args)
{
int arr[] = { 4 , 4 , 1 , 2 , 1 , 4 };
int n = arr.length;
System.out.print(maxDistance(arr, n));
}
}
|
Python3
def maxDistance(arr, n):
if (arr[ 0 ] ! = arr[n - 1 ]):
return (n - 1 );
i = n - 1 ;
while (i > 0 ):
if (arr[i] ! = arr[ 0 ]):
break ;
i - = 1 ;
distFirst = - 1 if (i = = 0 ) else i;
i = 0 ;
while (i < n - 1 ):
if (arr[i] ! = arr[n - 1 ]):
break ;
i + = 1 ;
distLast = - 1 if (i = = n - 1 ) else (n - 1 - i);
maxDist = max (distFirst, distLast);
return maxDist;
arr = [ 4 , 4 , 1 , 2 , 1 , 4 ];
n = len (arr);
print (maxDistance(arr, n));
|
C#
using System;
class GFG
{
static int maxDistance( int []arr, int n)
{
if (arr[0] != arr[n - 1])
return (n - 1);
int i = n - 1;
while (i > 0)
{
if (arr[i] != arr[0])
break ;
i--;
}
int distFirst = (i == 0) ? -1 : i;
i = 0;
while (i < n - 1)
{
if (arr[i] != arr[n - 1])
break ;
i++;
}
int distLast = (i == n - 1) ? -1 : (n - 1 - i);
int maxDist = Math.Max(distFirst, distLast);
return maxDist;
}
static public void Main ()
{
int []arr = { 4, 4, 1, 2, 1, 4 };
int n = arr.Length;
Console.WriteLine(maxDistance(arr, n));
}
}
|
Javascript
<script>
function maxDistance(arr, n)
{
if (arr[0] != arr[n - 1])
return (n - 1);
var i = n - 1;
while (i > 0) {
if (arr[i] != arr[0])
break ;
i--;
}
var distFirst = (i == 0) ? -1 : i;
i = 0;
while (i < n - 1) {
if (arr[i] != arr[n - 1])
break ;
i++;
}
var distLast = (i == n - 1) ? -1 : (n - 1 - i);
var maxDist = Math.max(distFirst, distLast);
return maxDist;
}
var arr = [ 4, 4, 1, 2, 1, 4 ];
var n = arr.length;
document.write( maxDistance(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...