/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static int getNumberSameAsIndex(int[] numbers) {
if(numbers == null || numbers.length == 0) {
return -1;
}
int left = 0;
int right = numbers.length - 1;
while(left <= right) {
int middle = left + ((right - left) >>> 1);
if(numbers[middle] == middle) {
return middle;
}
if(numbers[middle] > middle) {
right = middle - 1;
}
else {
left = middle + 1;
}
}
return -1;
}
// ----------------------- TEST CODE -----------------------
private static void test
(String testName,
int[] numbers,
int expected
) { if(getNumberSameAsIndex(numbers) == expected) {
System.
out.
println(testName
+ " passed."); }
else {
System.
out.
println(testName
+ " FAILED."); }
}
private static void test1() {
int[] numbers = {-3, -1, 1, 3, 5};
int expected = 3;
test("test1", numbers, expected);
}
private static void test2() {
int[] numbers = {0, 1, 3, 5, 6};
int expected = 0;
test("test2", numbers, expected);
}
private static void test3() {
int[] numbers = {-1, 0, 1, 2, 4};
int expected = 4;
test("test3", numbers, expected);
}
private static void test4() {
int[] numbers = {-1, 0, 1, 2, 5};
int expected = -1;
test("test4", numbers, expected);
}
private static void test5() {
int[] numbers = {0};
int expected = 0;
test("test5", numbers, expected);
}
private static void test6() {
int[] numbers = {10};
int expected = -1;
test("test6", numbers, expected);
}
test1();
test2();
test3();
test4();
test5();
test6();
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKICAgIHB1YmxpYyBzdGF0aWMgaW50IGdldE51bWJlclNhbWVBc0luZGV4KGludFtdIG51bWJlcnMpIHsKICAgICAgICBpZihudW1iZXJzID09IG51bGwgfHwgbnVtYmVycy5sZW5ndGggPT0gMCkgewogICAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIGludCBsZWZ0ID0gMDsKICAgICAgICBpbnQgcmlnaHQgPSBudW1iZXJzLmxlbmd0aCAtIDE7CiAgICAgICAgd2hpbGUobGVmdCA8PSByaWdodCkgewogICAgICAgICAgICBpbnQgbWlkZGxlID0gbGVmdCArICgocmlnaHQgLSBsZWZ0KSA+Pj4gMSk7CiAgICAgICAgICAgIGlmKG51bWJlcnNbbWlkZGxlXSA9PSBtaWRkbGUpIHsKICAgICAgICAgICAgICAgIHJldHVybiBtaWRkbGU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIGlmKG51bWJlcnNbbWlkZGxlXSA+IG1pZGRsZSkgewogICAgICAgICAgICAgICAgcmlnaHQgPSBtaWRkbGUgLSAxOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgbGVmdCA9IG1pZGRsZSArIDE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgCiAgICAgICAgcmV0dXJuIC0xOwogICAgfQogICAgCiAgICAvLyAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSBURVNUIENPREUgLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0KICAgIHByaXZhdGUgc3RhdGljIHZvaWQgdGVzdChTdHJpbmcgdGVzdE5hbWUsIGludFtdIG51bWJlcnMsIGludCBleHBlY3RlZCkgewogICAgICAgIGlmKGdldE51bWJlclNhbWVBc0luZGV4KG51bWJlcnMpID09IGV4cGVjdGVkKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbih0ZXN0TmFtZSArICIgcGFzc2VkLiIpOwogICAgICAgIH0KICAgICAgICBlbHNlIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKHRlc3ROYW1lICsgIiBGQUlMRUQuIik7Ci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