Ray 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 contains an integer 'n', the size of array 'a'.
The second line contains 'n' space-separated integers a[i].
Output:
Print a single 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
ILLUSTRATION
Morae Q!
- Find the number of strings made by using each alphabet as starting character.
- Find the Pythagorean triplet.
- Find out what is the minimum possible energy he needs to spend.
- Sort the array in non-decreasing order and print out the original indices of sorted array.
- Compute the number of landmasses on the planet after all the meteorites have fallen.
- Give the appropriate server status as output.
- Regular expressions (Regex) using search module in python.
- Find the minimum distance between any pair of equal elements in the array.
- Find the total number of matching pairs of socks that are available.
- Find the total number of teams which can work together and cannot work together.
- Given the heights of all the boys and girls tell whether it is possible for all boys to get a girl.
- Find the sequence of cities to visit according to coordinates and conditions.
- Find the number of unique patches of rectangular land to grow samba(rice) in.
- Regular expression Regex matching strings.
- Generate a greeting quote for admin.
- Find all the cavities on the map and replace their depths with the character X.
- Check whether the given graph is Bipartite or not.
- Find the status of the passengers and safari cars at zoo after k units of time.
- Determine the chair number occupied by the child who will receive that chocolate.
- Check if Rubik’s cube of any dimensions can be assembled.