public static void Main()
var arrayWithMatch = new int[] { -40, -20, -1, 1, 2, 3, 4, 7, 9, 12, 13 };
var arrayWithoutMatch = new int[] { -40, -20, -1, 1, 2, 3, 4, 8, 9, 12, 13 };
Console.WriteLine(FindValueThatMatchesIndex(arrayWithMatch) == 7 ? "PASSED" : "FAILED");
Console.WriteLine(FindValueThatMatchesIndex(arrayWithoutMatch) == -1 ? "PASSED" : "FAILED");
public static int FindValueThatMatchesIndex(int[] numbers)
result = IndexCompare(numbers, 0, numbers.Length - 1, (numbers.Length / 2));
public static int IndexCompare(int[] numbers, int start, int end, int index)
if (numbers[index] == index)
if (index < numbers[index])
var newIndex = ((index - start) / 2) + start;
return IndexCompare(numbers, start, index, newIndex);
if (index > numbers[index])
var newIndex = ((end - index)/2) + index;
return IndexCompare(numbers, index, end, newIndex);