// is allowed to return null when result is empty // ofs is added to elements of b prior to comparison static int[] intersectSortedIntArrays_ofs_optimized(int[] a, int[] b, int ofs) { int i = 0, j = 0, la = l(a), lb = l(b); // special case zero elements if (la == 0 || lb == 0) null; // special case one element if (la == 1) ret Arrays.binarySearch(b, a[0]-ofs) >= 0 ? a : null; if (lb == 1) ret Arrays.binarySearch(a, b[0]+ofs) >= 0 ? b : null; IntBuffer buf = new(min(la, lb)); while (i < la && j < lb) { int x = a[i], y = b[j]+ofs; if (x == y) { buf.add(x); ++i; ++j; } else if (x < y) ++i; else ++j; } ret buf.toArray(); }