-
Notifications
You must be signed in to change notification settings - Fork 0
/
smallest-difference.cpp
155 lines (114 loc) · 3.48 KB
/
smallest-difference.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* Smallest Difference: Given two arrays of integers, compute the pair of values (one value in each
array) with the smallest (non-negative) difference. Return the difference.
EXAMPLE
Input: {l, 3, 15, 11, 2}, {23, 127, 235, 19, 8}
Output: 3. That is, the pair (11, 8).
Brute Force
The simple brute force way is to just iterate through all pairs, compute the difference, and compare it
to the current minimum difference.
int findSmallestDifference(int[] arrayl, int[] array2) {
if (arrayl.length == 0 I I array2.length == 0) return -1;
int min = Integer.MAX_VALUE;
for (inti= 0; i < arrayl.length; i++) {
for (int j = 0; j < array2.length; j++) {
if (Math.abs(arrayl[i] - array2[j]) < min) {
min = Math.abs(arrayl[i] - array2[j]);
}
}
}
Optimal
A more optimal approach is to sort the arrays. Once the arrays are sorted, we can find the minimum
difference by iterating through the array.
Consider the following two arrays:
A: {l, 2, 11, 15}
B: {4, 12, 19, 23, 127, 235}
Try the following approach:
1. Suppose a pointer a points to the beginning of A and a pointer b points to the beginning of B. The
current difference between a and b is 3. Store this as the min.
2. How can we (potentially) make this difference smaller? Well, the value at b is bigger than the value
at a, so moving b will only make the difference larger. Therefore, we want to move a.
3. Now a points to 2 and b (still) points to 4. This difference is 2, so we should update min.
Move a, since it is smaller.
4. Now a points to 11 and b points to 4. Move b.
5. Now a points to 11 and b points to 12. Update min to 1. Move b.
And so on.
Time complexity = O(a*log(a) + b*log(b)) since both arrays need to be sorted first.
*/
#include <iostream>
#include <limits>
using namespace std;
int imin = std::numeric_limits<int>::min(); // minimum value
int imax = std::numeric_limits<int>::max();
void mergeSortedArrays(int* arr, int low, int high, int mid) {
int mergedArrayLength = high - low + 1;
int i, j, k, c[mergedArrayLength];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
{
if (arr[i] < arr[j])
{
c[k] = arr[i];
k++;
i++;
}
else
{
c[k] = arr[j];
k++;
j++;
}
}
while (i <= mid) {
c[k] = arr[i];
k++;
i++;
}
while (j <= high) {
c[k] = arr[j];
k++;
j++;
}
for(i = low; i < k; i++) {
arr[i] = c[i];
}
}
void mergeSort(int* arr, int low, int high) {
if(low < high) {
int mid = (high + low) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
mergeSortedArrays(arr, low, high, mid);
}
}
int findSmallestDifference(int* arr1, int size1, int* arr2, int size2) {
mergeSort(arr1, 0, size1 - 1);
mergeSort(arr2, 0, size2 - 1);
int min = imax;
int i = 0;
int j = 0;
int diff;
while(i < size1 && j < size2) {
if(arr1[i] >= arr2[j]) {
diff = arr1[i] - arr2[j];
if(diff < min) {
min = diff;
}
j++;
}
else if(arr1[i] < arr2[j]) {
diff = arr2[j] - arr1[i];
if(diff < min) {
min = diff;
}
i++;
}
}
return min;
}
int main() {
int arr1[5] = { 1, 3, 15, 11, 2 };
int arr2[5] = { 23, 127, 235, 19, 8 };
cout<<findSmallestDifference(arr1, 5, arr2, 5);
}