Maximum distance between two occurrences of same element in array in C

In this problem, we need to find the maximum distance between two occurrences of the same element in an array. The distance is calculated as the number of elements between the first and last occurrence of any repeating element.

Syntax

int maxDistance(int arr[], int n);

Algorithm

  • Traverse each element of the array
  • For each element, find its last occurrence in the remaining array
  • Calculate the distance between first and last occurrence (j - i - 1)
  • Keep track of the maximum distance found
  • Return -1 if no repeating elements exist

Example 1: Basic Implementation

Here's a simple approach using nested loops to find the maximum distance −

#include <stdio.h>

int maxDistance(int arr[], int n) {
    int maxD = -1;
    
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (arr[i] == arr[j]) {
                int temp = j - i - 1;
                if (temp > maxD) {
                    maxD = temp;
                }
            }
        }
    }
    return maxD;
}

int main() {
    int arr[] = {1, 2, 4, 1, 3, 4, 2, 5, 6, 5};
    int n = 10;
    
    printf("Array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("
"); int result = maxDistance(arr, n); printf("Maximum distance between two occurrences: %d
", result); return 0; }
Array: 1 2 4 1 3 4 2 5 6 5 
Maximum distance between two occurrences: 4

Example 2: Test Case with No Repeating Elements

Let's test the function when there are no repeating elements −

#include <stdio.h>

int maxDistance(int arr[], int n) {
    int maxD = -1;
    
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (arr[i] == arr[j]) {
                int temp = j - i - 1;
                if (temp > maxD) {
                    maxD = temp;
                }
            }
        }
    }
    return maxD;
}

int main() {
    int arr1[] = {10, 20, 1, 10, 10, 21, 12, 0};
    int arr2[] = {1, 2, 3, 4, 5}; // No repeating elements
    
    printf("Test Case 1: Maximum distance = %d
", maxDistance(arr1, 8)); printf("Test Case 2: Maximum distance = %d
", maxDistance(arr2, 5)); return 0; }
Test Case 1: Maximum distance = 3
Test Case 2: Maximum distance = -1

How It Works

For the array {1, 2, 4, 1, 3, 4, 2, 5, 6, 5}:

  • Element 1: First at index 0, last at index 3. Distance = 3 - 0 - 1 = 2
  • Element 2: First at index 1, last at index 6. Distance = 6 - 1 - 1 = 4
  • Element 4: First at index 2, last at index 5. Distance = 5 - 2 - 1 = 2
  • Element 5: First at index 7, last at index 9. Distance = 9 - 7 - 1 = 1

The maximum distance is 4 (for element 2).

Time Complexity

  • Time Complexity: O(n²) due to nested loops
  • Space Complexity: O(1) as we use only constant extra space

Conclusion

This algorithm efficiently finds the maximum distance between two occurrences of the same element using a nested loop approach. It returns -1 when no repeating elements exist, making it robust for all test cases.

Updated on: 2026-03-15T13:01:39+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements