Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created April 25, 2026 08:29
Show Gist options
  • Select an option

  • Save thinkphp/a16a969066deaa08e22955fdd52ce2b0 to your computer and use it in GitHub Desktop.

Select an option

Save thinkphp/a16a969066deaa08e22955fdd52ce2b0 to your computer and use it in GitHub Desktop.
find-duplicate-nlogn-binary-search.cpp
int findDuplicate(vector<int>_&nums) {
int low = 1;
int high = nums.size() - 1;// 5= 5-1= 4
[1,2,3,4]
while(low < high) {
int mid = (low + high) / 2;
int count = 0;
for(int num: nums) {
if(num <= mid) {
count++;
}
}
if(count > mid) high = mid;
else
low = mid + 1;
}
return low;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment