Created
April 25, 2026 08:29
-
-
Save thinkphp/a16a969066deaa08e22955fdd52ce2b0 to your computer and use it in GitHub Desktop.
find-duplicate-nlogn-binary-search.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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