/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
Distributed under the BSD license.
(See accompanying file LICENSE.txt at
https://g...content-available-to-author-only...b.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
*******************************************************************/
#include <iostream>
int countRange(const int* numbers, int length, int start, int end);
int getDuplication(const int* numbers, int length)
{
if(numbers == nullptr || length <= 0)
return -1;
int start = 1;
int end = length - 1;
while(end >= start)
{
int middle = ((end - start) >> 1) + start;
int count = countRange(numbers, length, 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;
}
int countRange(const int* numbers, int length, int start, int end)
{
if(numbers == nullptr)
return 0;
int count = 0;
for(int i = 0; i < length; i++)
if(numbers[i] >= start && numbers[i] <= end)
++count;
return count;
}
void test(const char* testName, int* numbers, int length, int* duplications, int dupLength)
{
int result = getDuplication(numbers, length);
for(int i = 0; i < dupLength; ++i)
{
if(result == duplications[i])
{
std::cout << testName << "result is: " << result << ", passed." << std::endl;
return;
}
}
std::cout << testName << " FAILED." << std::endl;
}
void test1()
{
int numbers[] = { 2, 2, 3, 3, 5, 5, 6, 6 };
int duplications[] = { 2, 3, 5, 6 };
test("test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}
int main()
{
test1();
return 0;
}
LyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKQ29weXJpZ2h0KGMpIDIwMTYsIEhhcnJ5IEhlCkFsbCByaWdodHMgcmVzZXJ2ZWQuCgpEaXN0cmlidXRlZCB1bmRlciB0aGUgQlNEIGxpY2Vuc2UuCihTZWUgYWNjb21wYW55aW5nIGZpbGUgTElDRU5TRS50eHQgYXQKaHR0cHM6Ly9nLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5iLmNvbS96aGVkYWhodC9Db2RpbmdJbnRlcnZpZXdDaGluZXNlMi9ibG9iL21hc3Rlci9MSUNFTlNFLnR4dCkKKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKi8KCgojaW5jbHVkZSA8aW9zdHJlYW0+CgppbnQgY291bnRSYW5nZShjb25zdCBpbnQqIG51bWJlcnMsIGludCBsZW5ndGgsIGludCBzdGFydCwgaW50IGVuZCk7CgppbnQgZ2V0RHVwbGljYXRpb24oY29uc3QgaW50KiBudW1iZXJzLCBpbnQgbGVuZ3RoKQp7CiAgICBpZihudW1iZXJzID09IG51bGxwdHIgfHwgbGVuZ3RoIDw9IDApCiAgICAgICAgcmV0dXJuIC0xOwoKICAgIGludCBzdGFydCA9IDE7CiAgICBpbnQgZW5kID0gbGVuZ3RoIC0gMTsKICAgIHdoaWxlKGVuZCA+PSBzdGFydCkKICAgIHsKICAgICAgICBpbnQgbWlkZGxlID0gKChlbmQgLSBzdGFydCkgPj4gMSkgKyBzdGFydDsKICAgICAgICBpbnQgY291bnQgPSBjb3VudFJhbmdlKG51bWJlcnMsIGxlbmd0aCwgc3RhcnQsIG1pZGRsZSk7CiAgICAgICAgaWYoZW5kID09IHN0YXJ0KQogICAgICAgIHsKICAgICAgICAgICAgaWYoY291bnQgPiAxKQogICAgICAgICAgICAgICAgcmV0dXJuIHN0YXJ0OwogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICB9CgogICAgICAgIGlmKGNvdW50ID4gKG1pZGRsZSAtIHN0YXJ0ICsgMSkpCiAgICAgICAgICAgIGVuZCA9IG1pZGRsZTsKICAgICAgICBlbHNlCiAgICAgICAgICAgIHN0YXJ0ID0gbWlkZGxlICsgMTsKICAgIH0KICAgIHJldHVybiAtMTsKfQoKaW50IGNvdW50UmFuZ2UoY29uc3QgaW50KiBudW1iZXJzLCBpbnQgbGVuZ3RoLCBpbnQgc3RhcnQsIGludCBlbmQpCnsKICAgIGlmKG51bWJlcnMgPT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