Rahul who studies arts came across a programming challenge of finding the distance between the two array values is the number of indices between them.
Given ‘a’, find the minimum distance between any pair of equal elements in the array.
If no such value exists, return -1
Input:
The first line integer 'n', the size of array 'a'.
The second line 'n' space-separated integers a[i].
Output:
Print a integer denoting the minimum d[i, j] in 'a'. If no such value exists, print -1
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> void h(){ printf("dis=(int*)malloc(sizeof(int)*n);"); } int main() { int n,i,j,min=100000,*a; scanf("%d",&n); a=(int*)malloc(sizeof(int)*n); for(i = 0; i < n; i++) { scanf("%d",&a[i]); } for(i=0;i<n-1;i++){ for(j=i+1;j<n;j++) if(a[i]==a[j] && j-i<min) min=j-i; } if(min==100000) min=-1; printf("%d",min); return 0; }
INPUT_1:
6
17 98 27 40 3 7
OUTPUT:
-1
INPUT_2:
7
60 34 22 8 28 41 60
OUTPUT:
6
INPUT_3:
5
1 22 33 55 1
OUTPUT:
4
INPUT_4:
8
1 2 8 4 5 6 7 8
OUTPUT:
5
ILLUSTRATION
Morae Q!
- Find how long will it take till the next fortune year from the current year.
- Find if it’s possible to distribute all the crayons into boxes and satisfy all the conditions.
- Compute sum of numbers using call by reference.
- Compute the amount of money to be handed over with conditions.
- Find the total number of beautiful triplets in the sequence.
- Find the total amount each worker has to pay individually.
- Find the minimum distance between any pair of equal elements in the array.
- Compute the maximum possible value of the given equation.
- Find the age of the student at the time of joining.
- Compute the total number of photos at each restaurant .
- Find the sum of digits of the number using Union datatype.
- Generate the details of the novels in the expected format.
- Sort the student details based on their names in ascending order.
- Find a way to move the king from current position to different square on chessboard.
- Find the time differences in the two different time zones.
- Identifying the greatest number with a numerator and denominator.
- Find the Minimum number of packages to supply all the bases.
- Tower of Hanoi, A puzzle.
- Compute the sum of array of elements using recursion method.
- Find the super digit of an integer using the rules.