-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Distances.java
More file actions
104 lines (92 loc) · 3.02 KB
/
Minimum_Distances.java
File metadata and controls
104 lines (92 loc) · 3.02 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'minimumDistances' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY a as parameter.
*/
public static int minimumDistances(List<Integer> a)
{
// Write your code here
/*int i,j;
int res = Integer.MAX_VALUE;
for(i=0;i<a.size();i++)
{
for(j=i+1;j<a.size();j++)
{
if(a.get(i)==a.get(j))
{
int dif = j-i; //calculating the difference of the index value.
res = Math.min(res,dif); //this will be happening for the min value..
}
}
}
if(res==Integer.MAX_VALUE)
{
return -1;
}
return res;*/
//create a map which will store the index of the respective keys..
Map<Integer,List<Integer>> M = new HashMap<>();
int i;
for(i=0;i<a.size();i++)
{
if(M.containsKey(a.get(i)))
{
M.get(a.get(i)).add(i);
}
else //if map does not contain the key..
{
List<Integer> l = new ArrayList<>();
l.add(i);
M.put(a.get(i),l);
}
}
//System.out.println(M);
int min = Integer.MAX_VALUE;
for(Map.Entry<Integer,List<Integer>> entry:M.entrySet())
{
List<Integer> D = entry.getValue(); //taking a list at each instance..
if(D.size()<=1) //atmost 1 value is there in the list..
{
continue;
}
Collections.sort(D); //sort the list..
for(i=0;i<D.size()-1;i++)
{
min = Math.min(min,D.get(i+1)-D.get(i)); //check the minimum difference..
}
}
if(min==Integer.MAX_VALUE) //if no pair is there...(that is only one value is there..)
{
return -1;
}
return min;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int result = Result.minimumDistances(a);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}