actually it doesn't work. i wrote another solution.
static int GetMin(int[] a, int[] b)
{
Array.Sort(a, 0, a.Length);
Array.Sort(b, 0, b.Length);
int max = int.MaxValue - 1;
int length = Math.Min(a.Length, b.Length);
if (length > 0 && a[0] > 1 && b[0] > 1)
return 1;
for (int i = 0; i < length; i++)
{
int x = a[i];
int y = b[i];
// current values may be too bigger than previous max
if (max + 1 < x && max + 1 < y)
return max + 1;
if (Math.Abs(x - y) > 1)
{
int min = Math.Min(x, y) + 1;
// one of the next items may be smaller than min
if (i == length - 1 || i < length - 1 && min < a[i + 1] && min < b[i + 1])
return min;
}
int currmax = Math.Max(x, y);
if (x > max + 1 && y > max + 1)
{
// one of the next items may be smaller than current max
if (i == length - 1 || i < length - 1 && currmax < a[i + 1] && currmax < b[i + 1])
return max + 1;
}
else
max = currmax;
}
return -1;
}
this should handle all cases and passes these tests. this should be the fastest possible solution
Assert.True(GetMin(new int[] { 1, 2, 4, 8 }, new int[] { 1, 3, 7, 8 }) == 5);
Assert.True(GetMin(new int[] { 1, 3, 4 }, new int[] { 1, 7, 8 }) == 2);
Assert.True(GetMin(new int[] { 1, 2, 3 }, new int[] { 1, 2, 5 }) == 4);
Assert.True(GetMin(new int[] { 1, 5 }, new int[] { 1, 4 }) == 2);
Assert.True(GetMin(new int[] { 1, 5 }, new int[] { 3, 4 }) == 2);
Assert.True(GetMin(new int[] { 1, 3, 6 }, new int[] { 2, 3, 7 }) == 4);
Assert.True(GetMin(new int[] { 1, 2, 6 }, new int[] { 5, 7, 8, 7 }) == 3);
Assert.True(GetMin(new int[] { 3, 4, 6 }, new int[] { 5, 7, 8, 7 }) == 1);
Assert.True(GetMin(new int[] { 3 }, new int[] { 1 }) == 2);