forked from lemonbashar/java-algo-expert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallestDifference.java
More file actions
66 lines (52 loc) · 1.61 KB
/
SmallestDifference.java
File metadata and controls
66 lines (52 loc) · 1.61 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
package algoexpert.medium;
import java.util.Arrays;
/*
PROBLEM:
Find numbers from two different arrays whose absolute difference is smallest
Logic:
[a, a+1, a+2]
[b, b+1, b+2]
Solution:
1. Brute Force -> t : O(nm) | s : O(1)
2. Sort & two pointer -> t : O(nlogn + mlogm) | s: O(1)
*/
public class SmallestDifference
{
public static void test()
{
int[] arr1 = {-1, 5, 10, 20, 28, 3};
int[] arr2 = {26, 134, 135, 15, 17};
int[] sol = smallestDifference(arr1, arr2);
System.out.println("[ " + sol[0] + ", " + sol[1] + " ]");
}
// time : O(nlogn + mlogm) | space: O(1)
public static int[] smallestDifference(int[] arrayOne, int[] arrayTwo)
{
if (arrayOne.length < 1 || arrayTwo.length < 1) { return new int[] {}; }
Arrays.sort(arrayOne);
Arrays.sort(arrayTwo);
int[] smallestDiffNum = { arrayOne[0], arrayTwo[0] };
int smallestDiff = Math.abs(arrayOne[0] - arrayTwo[0]);
int firstP = 0;
int secondP = 0;
while (firstP < arrayOne.length && secondP < arrayTwo.length)
{
int currentDiff = Math.abs(arrayOne[firstP] - arrayTwo[secondP]);
if (currentDiff < smallestDiff)
{
smallestDiff = currentDiff;
smallestDiffNum[0] = arrayOne[firstP];
smallestDiffNum[1] = arrayTwo[secondP];
}
if ( arrayOne[firstP] < arrayTwo[secondP] )
{
firstP += 1;
}
else
{
secondP += 1;
}
}
return smallestDiffNum;
}
}