Skip to content

Commit fb5aa21

Browse files
committed
Find Intersection
1 parent b07b402 commit fb5aa21

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/easy/FindIntersection.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package easy;
2+
3+
/**
4+
* Have the function FindIntersection(strArr) read the array of strings
5+
* stored in strArr which will contain 2 elements: the first element
6+
* will represent a list of comma-separated numbers sorted in ascending order,
7+
* the second element will represent a second list of comma-separated numbers (also sorted).
8+
* Your goal is to return a comma-separated string containing
9+
* the numbers that occur in elements of strArr in sorted order.
10+
* If there is no intersection, return the string false.
11+
*/
12+
public class FindIntersection {
13+
14+
/**
15+
* Binary Search helper function.
16+
*
17+
* @param coll an array of integers
18+
* @param key a value to find
19+
* @return index of the key
20+
*/
21+
private static int binarySearch(Integer[] coll, Integer key) {
22+
int lo = 0;
23+
int hi = coll.length - 1;
24+
while (lo <= hi) {
25+
int mid = lo + (hi - lo) / 2;
26+
if (key < coll[mid]) {
27+
hi = mid - 1;
28+
} else if (key > coll[mid]) {
29+
lo = mid + 1;
30+
} else {
31+
return mid;
32+
}
33+
}
34+
return -1;
35+
}
36+
37+
/**
38+
* Convert a string to an array of integers.
39+
*
40+
* @param strArr comma-delimited string of numbers
41+
* @return array of integers
42+
*/
43+
private static Integer[] toIntArray(String strArr) {
44+
String[] tmpArr = strArr.split(", ");
45+
Integer[] intArr = new Integer[tmpArr.length];
46+
for (int i = 0; i < tmpArr.length; i++) {
47+
intArr[i] = Integer.parseInt(tmpArr[i].trim());
48+
}
49+
return intArr;
50+
}
51+
52+
/**
53+
* Find Intersection function.
54+
*
55+
* @param arr input array containing two strings of comma-delimited integers
56+
* @return the numbers that occur in elements of strArr in sorted order.
57+
*/
58+
private static String findIntersection(String[] arr) {
59+
Integer[] arr1 = toIntArray(arr[0]);
60+
Integer[] arr2 = toIntArray(arr[1]);
61+
StringBuilder builder = new StringBuilder();
62+
for (int i : arr1.length > arr2.length ? arr2 : arr1) {
63+
int findIndex = binarySearch(arr1.length > arr2.length ? arr1 : arr2, i);
64+
if (findIndex > -1) {
65+
builder.append(i).append(",");
66+
}
67+
}
68+
String result = builder.toString();
69+
return result.length() == 0 ? "false" : builder.substring(0, result.length() - 1);
70+
}
71+
72+
/**
73+
* Entry point.
74+
*
75+
* @param args command line arguments
76+
*/
77+
public static void main(String[] args) {
78+
var result1 = findIntersection(new String[]{"11, 12, 14, 16, 20", "11, 12, 13, 18, 21"});
79+
System.out.println(result1);
80+
var result2 = findIntersection(new String[]{"1, 3, 4, 7, 13", "1, 2, 4, 13, 15"});
81+
System.out.println(result2);
82+
var result3 = findIntersection(new String[]{"21, 22, 23, 25, 27, 28", "21, 24, 25, 29"});
83+
System.out.println(result3);
84+
}
85+
86+
}

0 commit comments

Comments
 (0)