Files

17 lines
541 B
Org Mode
Raw Permalink Normal View History

#+ANKI_DECK: study_deck_02
* Task: Two Sum complement lookup with lower_bound :cpp:binary-search:two-sum:retrieval::production:
:PROPERTIES:
:END:
** 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
#+begin_src cpp
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)};
}
#+end_src