http://www.geeksforgeeks.org/find-the-minimum-distance-between-two-numbers/
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <queue> 5 #include <stack> 6 #include <string> 7 #include <fstream> 8 #include <map> 9 using namespace std; 10 11 int mindis(int arr[], int n, int x, int y) { 12 int pre = 0; 13 while (arr[pre] != x && arr[pre] != y) pre++; 14 int i = pre + 1; 15 int ans = n; 16 for (; i < n; i++) { 17 if (arr[i] != x && arr[i] != y) continue; 18 if (arr[i] != arr[pre]) ans = min(ans, i - pre); 19 pre = i; 20 } 21 return ans; 22 } 23 24 int main() { 25 int arr[12] = {3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3}; 26 cout << mindis(arr, 12, 3, 6) << endl; 27 return 0; 28 }