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


We are given with an array of integers of size N. The array consists of integers in random order. The task is to find the maximum difference between the first and last indexes of an element in the array. We have to find a number which appears twice in the array and the difference between its indexes is maximum. If there are more such pairs then we will store the maximum such difference between the indexes.

Input 

Arr[] = { 2,1,3,1,3,2,5,5 }.

Output −Maximum difference between first and last indexes of an element in array − 5

Explanation − The pairs of element and difference between their indexes are as follows −

(2,2) Arr[0] and Arr[5] 5-0=5 max difference till now is 5
(1,1) Arr[1] and Arr[3] 3-1=2 max difference till now is 5
(3,3) Arr[2] and Arr[4] 4-2=2 max difference till now is 5
(5,5) Arr[6] and Arr[7] 7-6=1 max difference till now is 5

Input 

Arr[] = { 2,2,3,4,8,3,4,4,8,7 }.

Output −Maximum difference between first and last indexes of an element in array − 4

Explanation − The pairs of element and difference between their indexes are as follows −

(2,2) Arr[0] and Arr[1] ; 1-0=1; max difference till now is 1
(3,3) Arr[2] and Arr[5] ; 5-2=3; max difference till now is 3
(4,4,4) Arr[3],Arr[6],Arr[7] ; 7-6=1,6-3=3,7-3=4; max difference till now is 4
(8,8) Arr[4] and Arr[8] ; 8-4=4 ; max difference till now is 4

Approach used in the below program is as follows

  • Declare an array of integers which contains repeated numbers in random order.( Arr[] )

  • Create a variable to store the size of the array. (N)

  • The function maxDifference(int Arr[],int n) is used to calculate the maximum difference (maxD) between first and last indexes of an element in an array.

  • Inside maxDifference() we declared maxD is used to store the maximum indexes difference found so far.

  • Starting from the first element ( index i=0 ) traverse the array using for loop.

  • In nested for loop traverse the remaining array (j=i+1) till the last index is reached.

  • If we will find the same element as Arr[i] we calculate the difference between its indexes i,j and if it is more than the previous value of maxD, we update maxD.

  • Continue this till the end of both for loops.

  • Return the result stored in maxD.

Example

#include <stdio.h>
int maxDifference(int arr[],int n){
   int maxD=0;
   for(int i=0;i<n-1;i++){
      for(int j=i+1;j<n;j++){
         if(arr[i]==arr[j] && (j-i)>maxD)
            maxD=j-i;
      }
   }
   return maxD;
}
int main(){
   int Arr[] = {1, 4, 1, 3, 3, 5, 4, 5, 2};
   int N = sizeof(Arr) / sizeof(Arr[0]);
   printf("Maximum difference between first and last indexes of an element in array : %d" ,maxDifference(Arr, N);
   return 0;
}

Output

If we run the above code it will generate the following output −

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

Updated on: 14-Aug-2020

459 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements