Files
cpp-flashcards/org/study_deck_02/dsa/binary-search/two-sum-lower-bound.org
T
tomatocream e10cc4257d add flashcard generation tooling and binary search cards
- 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
2026-06-08 11:38:09 +08:00

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)};
}