-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathminimum_distance_between_two_numbers.java
51 lines (49 loc) · 1.75 KB
/
minimum_distance_between_two_numbers.java
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
/**
* http://www.geeksforgeeks.org/find-the-minimum-distance-between-two-numbers/
*/
//time complexity : O(n)
/*
STEPS:
1) Traverse array from left side and stop if either x or y is found. Store index of this first occurrence
in a variable say prev.
2) Now traverse arr[] after the index prev. If the element at current index i matches with either x or y then check if it is different from arr[prev]. If it is different then update the minimum distance if needed.
If it is same then update prev i.e., make prev = i.
*/
public class MinimumDistanceBetweenTwoNumbers {
public int minDistance(int input[],int x, int y){
int prev = -1;
int prevFound = -1;
int min = 10000;
for(int i=0; i < input.length; i++){
if(input[i] == x){
if(prevFound == -1){
prevFound = x;
prev = i;
}else if(prevFound == x){
prev = i;
}else{
min = min > i - prev ? i -prev : min;
prev = i;
prevFound = x;
}
}else if(input[i] == y){
if(prevFound == -1){
prevFound = y;
prev = i;
}else if(prevFound == y){
prev =i;
}else{
min = min > i - prev ? i -prev : min;
prevFound = y;
prev = i;
}
}
}
return min;
}
public static void main(String args[]){
MinimumDistanceBetweenTwoNumbers mdb = new MinimumDistanceBetweenTwoNumbers();
int input[] = {6,4,1,5,6,9,10,4,6,6};
System.out.println(mdb.minDistance(input, 5, 6));
}
}