- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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.

#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; }

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

- Related Questions & Answers
- First element and last element in a JavaScript array?
- Find First and Last Position of Element in Sorted Array in Python
- Maximum length of the sub-array whose first and last elements are same in C++
- Find the maximum element in an array which is first increasing and then decreasing in C++
- Absolute difference between the first X and last X Digits of N?
- Difference between first and the second array in JavaScript
- C# program to find maximum and minimum element in an array
- Maximum possible difference of two subsets of an array in C++
- Maximum distance between two occurrences of same element in array in C
- Get the first and last item in an array using JavaScript?
- Product of maximum in first array and minimum in second in C
- Maximum difference between the group of k-elements and rest of the array in C
- Maximum Difference Between Node and Ancestor in C++
- Maximum possible XOR of every element in an array with another array in C++
- Difference between sum and product of an array in JavaScript

Advertisements