e10cc4257d
- gen-flashcards.py: auto-generate recognition cards from all problem files - toolkit/gen-problem-cards.org: 199 auto-generated problem cards - 5 binary search tool cards (std::binary_search, std::lower_bound, comparison, two-sum pattern, sorting gotcha) - two-sum.org: add binary search C++ attempt - lc-org.el: add doom emacs localleader keybinding support
541 B
541 B
Task: Two Sum complement lookup with lower_bound :cpp:binary-search:two-sum:retrieval::production:
Front
Given sorted nums and index i, write C++ to check if target - nums[i] exists in the rest of the array using std::lower_bound.
Back
int complement = target - nums[i];
auto it = std::lower_bound(nums.begin() + i + 1, nums.end(), complement);
if (it != nums.end() && *it == complement) {
return {(int)i, (int)std::distance(nums.begin(), it)};
}