Skip to content

Commit f8a4f79

Browse files
committed
Added solution for binary search in rotated arr and string with spared text
1 parent b7613d1 commit f8a4f79

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.eprogrammerz.examples.algorithm.crackingCoding;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
/**
8+
* Given a sorted array of n integers that has been rotated an unknown number of times,
9+
* write code to find an element in the array. You may assume that the array was originally sorted in increasing order.
10+
*
11+
* EXAMPLE
12+
* lnput:findSin{lS, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14} Output: 8 (the index of 5 in the array)
13+
*/
14+
public class SearchRotatedArr {
15+
public int search(int[] arr, int target) {
16+
int start = 0;
17+
int end = arr.length - 1;
18+
19+
while (start <= end) {
20+
int midIdx = start + (end - start) / 2;
21+
int mid = arr[midIdx];
22+
23+
if (mid == target) {
24+
return midIdx;
25+
}
26+
27+
if (arr[end] >= arr[start]) { // increasing sequence
28+
if (mid < target) {
29+
start = midIdx + 1;
30+
} else {
31+
end = midIdx - 1;
32+
}
33+
} else { // non-increasing sequence
34+
if (arr[start] > mid && target >= arr[start]) {
35+
end = midIdx - 1;
36+
} else {
37+
start = midIdx + 1;
38+
}
39+
}
40+
}
41+
return -1;
42+
}
43+
44+
@Test
45+
public void testSearchRotatedArr() {
46+
int[] arr = new int[]{15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14};
47+
assertEquals(8, search(arr, 5));
48+
assertEquals(2, search(arr, 19));
49+
assertEquals(0, search(arr, 15));
50+
assertEquals(1, search(arr, 16));
51+
assertEquals(11, search(arr, 14));
52+
assertEquals(4, search(arr, 25));
53+
assertEquals(-1, search(arr, 21));
54+
}
55+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.eprogrammerz.examples.algorithm.crackingCoding;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
8+
/**
9+
* Given a sorted array of strings that is interspersed with empty strings, write a method to find the location of a given string.
10+
* For example: {"at", "", "", "", "ball", "", "", "car", "", "", "dad", "", ""}
11+
* For target: "ball"
12+
* Output: 4
13+
*/
14+
public class SparseSearch {
15+
public int sparseSearch(String[] arr, String target) {
16+
int start = 0;
17+
int end = arr.length - 1;
18+
19+
while (start <= end) {
20+
int midIdx = start + (end - start) / 2;
21+
String mid = arr[midIdx];
22+
23+
if (mid.isEmpty()) {
24+
// traverse both back and front and determine start or end
25+
int left = midIdx - 1;
26+
int right = midIdx + 1;
27+
while (true) {
28+
if (left < start && right > end) return -1;
29+
30+
if (right <= end && !arr[right].isEmpty()) {
31+
midIdx = right;
32+
break;
33+
}
34+
35+
if (left >= start && !arr[left].isEmpty()) {
36+
midIdx = left;
37+
break;
38+
}
39+
40+
left--;
41+
right++;
42+
}
43+
}
44+
mid = arr[midIdx];
45+
46+
if (mid.equals(target)) return midIdx;
47+
48+
if (mid.compareTo(target) > 0) {
49+
end = midIdx - 1;
50+
} else {
51+
start = midIdx + 1;
52+
}
53+
54+
}
55+
return -1;
56+
}
57+
58+
@Test
59+
public void testSparseSearch() {
60+
String[] arr = new String[]{"at", "", "", "", "ball", "", "", "car", "", "", "dad", "", ""};
61+
assertEquals(0, sparseSearch(arr, "at"));
62+
assertEquals(4, sparseSearch(arr, "ball"));
63+
assertEquals(7, sparseSearch(arr, "car"));
64+
assertEquals(10, sparseSearch(arr, "dad"));
65+
assertEquals(-1, sparseSearch(arr, "yogen"));
66+
}
67+
}

0 commit comments

Comments
 (0)