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++
// C++ implementation of the approach
#include <bits/stdc++.h>
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[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 = max(distFirst, distLast);
return maxDist;
}
// Driver code
int main()
{
int arr[] = { 4, 4, 1, 2, 1, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxDistance(arr, n);
return 0;
}
Java
// 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..
Python3
# 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#
// 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[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
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..
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the maximum distance
// between two unequal elements
function maxDistance(arr, n)
{
// If first and last elements are unequal
// they are maximum distance apart
if (arr[0] != arr[n - 1])
return (n - 1);
var 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
var 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
var distLast = (i == n - 1) ? -1 : (n - 1 - i);
// Maximum possible distance
var maxDist = Math.max(distFirst, distLast);
return maxDist;
}
// Driver code
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)
Similar Reads
Maximum difference between two subsets of m elements Given an array of n integers and a number m, find the maximum possible difference between two sets of m elements chosen from given array.Examples: Input : arr[] = 1 2 3 4 5 m = 4Output : 4The maximum four elements are 2, 3, 4 and 5. The minimum four elements are 1, 2, 3 and 4. The difference between
6 min read
Maximum difference between two elements in an Array Given an array arr[] of N integers, the task is to find the maximum difference between any two elements of the array.Examples: Input: arr[] = {2, 1, 5, 3} Output: 4 |5 - 1| = 4 Input: arr[] = {-10, 4, -9, -5} Output: 14 Naive Approach:- As the maximum difference will be in between smallest and the l
9 min read
Maximum difference between nearest left and right smaller elements Given an array of integers, the task is to find the maximum absolute difference between the nearest left and the right smaller element of every element in the array. Note: If there is no smaller element on right side or left side of any element then we take zero as the smaller element. For example f
12 min read
Minimum distance between two occurrences of maximum You are given an array of n-elements with a basic condition that occurrence of greatest element is more than once. You have to find the minimum distance between maximums. (n>=2). Examples: Input : arr[] = {3, 5, 2, 3, 5, 3, 5} Output : Minimum Distance = 2 Explanation : Greatest element is 5 and
5 min read
Maximum difference between group of k-elements and rest of the array. You are given an array of n elements. You have to divide the given array into two group such that one group consists exactly k elements and second group consists rest of elements. Your result must be maximum possible difference of sum of elements of these two group. Examples: Input : arr[n] = {1, 5,
7 min read
Maximum difference between groups of size two Given an array of even number of elements, form groups of 2 using these array elements such that the difference between the group with highest sum and the one with lowest sum is maximum. Note: An element can be a part of one group only and it has to be a part of at least 1 group. Examples: Input : a
9 min read