-
-
Save DimanNe/4d0eb8c55dd3ef86b4fa780ad83d8537 to your computer and use it in GitHub Desktop.
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
#include <vector> | |
// 1st: 5 10 15 20 | |
// 2nd: 1 | |
// 2nd: 5 7 | |
// 2nd: 5 10 | |
// 2nd: 5 12 | |
// 2nd: 7 | |
// lower_bound(x) returns: >= x | |
// upper_bound(x) return > x | |
// equal_range(): [returns: >= x; return > x] | |
void Helper(std::vector<int> & Result, | |
const std::vector<int> &First, | |
const std::vector<int> &Second, | |
int & fi, | |
int & si) { | |
if(fi >= First.size()) | |
return; | |
const auto Pair = std::equal_range(Second.begin() + si, Second.end(), First[fi]); | |
const auto FirstLargerOrEq = Pair.first; | |
const auto FirstLarger = Pair.second; | |
if(FirstLargerOrEq == Second.end()) | |
return; | |
Result.push_back(*FirstLargerOrEq); | |
si = FirstLarger - Second.begin(); // std::distance | |
// for(; si != Second.size() && Second[si] <= First[fi]; ++si) | |
// if(Second[si] == First[fi]) | |
// if(Result.empty() || Result.back() != Second[si]) | |
// Result.push_back(Second[si]); | |
} | |
std::vector<int> EqualElems(const std::vector<int> &First, const std::vector<int> &Second) { | |
std::vector<int> Result; | |
for(int fi = 0, si = 0; fi != First.size() && si != Second.size();) { | |
Helper(Result, First, Second, fi, si); | |
Helper(Result, Second, First, si, fi); | |
} | |
return Result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment