**Write a Modular C Programming code to solve Maximum Distance 3, Given an array of ‘N’ elements, find the maximum distance between two elements of same occurrences.**

Given an array of ‘N’ elements, find the maximum distance between two elements of same occurrences.

**Input Format**

First line contains ‘N’ number of array elements.

Second Line contains N array elements separated by a space.

**Constraints**

N>=1 and N<=10^5

-10^5<=a[i]<=10^5

**Output Format**

Output the maximum distance between the same occurrence of the key.

**Sample Input 0**

```
-8
```

**Sample Output 0**

```
Invalid
```

**Sample Input 1**

```
12
3 2 1 2 1 4 5 8 6 7 4 2
```

**Sample Output 1**

```
10
```

**Explanation 1**

```
maximum distance for 2 is 11-1 = 10
maximum distance for 1 is 4-2 = 2
maximum distance for 4 is 10-5 = 5
```

**Refer :** C Programming HackerRank all solutions for Loops | Arrays | strings

**CODE:**

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> void read_a(int a[30],int n) { int i; for(i=0;i<n;i++) { scanf("%d",&a[i]); } } void dis_a(int a[30],int n) { int i,j,diff,max=0; for(i=0;i<n;i++) { for(j=n-1;j>-1;j--) { if(a[i]==a[j]) { diff=j-i; if(max<diff) { max=diff; } } } } printf("%d",max); } int main() { int n,a[30]; scanf("%d",&n); if(n<0||n>=100000) { printf("Invalid"); exit(0); } read_a(a,n); dis_a(a,n); return 0; }

**OUTPUT**

Congratulations, you passed the sample test case. Click the Submit Code button to run your code against all the test cases. Input (stdin) -8 Your Output (stdout) Invalid Expected Output Invalid

**Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:**

Top 100+ C Programming codes – KLE Technological University