
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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
- Related Articles
- First element and last element in a JavaScript array?
- Find difference between first and second largest array element in Java
- 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++\n
- Maximum distance between two occurrences of same element in array in C
- Maximum possible XOR of every element in an array with another array in C++
- Find the first repeating element in an array of integers C++
- C# program to find maximum and minimum element in an array\n
- Maximum possible difference of two subsets of an array in C++
- Product of maximum in first array and minimum in second in C
- Get the first and last item in an array using JavaScript?
- C# program to find the last matching element in an array
- Difference between first and the second array in JavaScript
- Maximum difference between the group of k-elements and rest of the array in C
