/* 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
{
private static int countRange(int[] numbers, int start, int end)
{
int count = 0;
for(int i = 0; i < numbers.length; i++)
if(numbers[i] >= start && numbers[i] <= end)
++count;
return count;
}
public static int getDuplication(int[] numbers)
{
int start = 1;
int end = numbers.length;
while(end >= start) {
int middle = ((end - start) >> 1) + start;
int count = countRange(numbers, start, middle);
if(end == start) {
if(count > 1)
return start;
else
break;
}
if(count > (middle - start + 1))
end = middle;
else
start = middle + 1;
}
return -1;
}
private static void test
(String testName,
int[] numbers,
int[] duplications
) {
int result = getDuplication(numbers);
for(int i = 0; i < duplications.length;++i)
{
if(result == duplications[i])
{
System.
out.
println(testName
+ " passed."); return;
}
}
System.
out.
println(testName
+ " FAILED."); }
private static void test1()
{
int[] numbers = {2, 3, 5, 4, 3, 2, 6, 7};
int[] duplications = {2, 3};
test("test1", numbers, duplications);
}
private static void test2()
{
int[] numbers = {3, 2, 1, 4, 4, 5, 6, 7};
int[] duplications = {4};
test("test2", numbers, duplications);
}
private static void test3()
{
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 1, 8};
int[] duplications = {1};
test("test3", numbers, duplications);
}
private static void test4()
{
int[] numbers = {1, 7, 3, 4, 5, 6, 8, 2, 8};
int[] duplications = {8};
test("test4", numbers, duplications);
}
private static void test5()
{
int[] numbers = {1, 1};
int[] duplications = {1};
test("test5", numbers, duplications);
}
private static void test6()
{
int[] numbers = {3, 2, 1, 3, 4, 5, 6, 7};
int[] duplications = {3};
test("test6", numbers, duplications);
}
private static void test7()
{
int[] numbers = {1, 2, 2, 6, 4, 5, 6};
int[] duplications = {2, 6};
test("test7", numbers, duplications);
}
{
test1();
test2();
test3();
test4();
test5();
test6();
test7();
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwpp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