-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathE0037.cpp
More file actions
43 lines (39 loc) · 760 Bytes
/
E0037.cpp
File metadata and controls
43 lines (39 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Problem Statement: https://www.hackerrank.com/challenges/minimum-distances/problem
*/
#include <iostream>
#include <vector>
#include <map>
#include <climits>
#include <iterator>
using namespace std;
int minimumDistances(vector<int> a) {
map<int, int> indices;
int minDist = INT_MAX;
for(int i=0 ; i<a.size() ; i++){
if(minDist == 1)
break;
map<int, int>::iterator it = indices.find(a[i]);
if(it != indices.end()){
int dist = i - it->second;
if(dist < minDist)
minDist = dist;
it->second = i;
}
else
indices[a[i]] = i;
}
return (minDist == INT_MAX) ? -1 : minDist;
}
int main()
{
int n, num;
vector<int> a;
cin>>n;
for(int i=0 ; i<n ; i++){
cin>>num;
a.push_back(num);
}
cout<<minimumDistances(a);
return 0;
}