forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearIn.java
More file actions
24 lines (21 loc) · 768 Bytes
/
linearIn.java
File metadata and controls
24 lines (21 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* Given two arrays of ints sorted in increasing order, outer and inner,
* return true if all of the numbers in inner appear in outer. The best
* solution makes only a single "linear" pass of both arrays, taking
* advantage of the fact that both arrays are already in sorted order.
*/
public boolean linearIn(int[] outer, int[] inner) {
int i = 0;
int j = 0;
while(i < inner.length && j < outer.length) {
if(inner[i] > outer[j]) {
j++;
} else if(inner[i] < outer[j]) {
return false;
} else {
i++;
}
}
if(i != inner.length)
return false;
return true;
}