Sobes.tech
Junior — Senior

Final array after merging

livecode

Task condition

After completing the main merge loop, it is necessary to add the remaining elements of one of the vectors to the final array, avoiding additional checks.

std::vector<int> merged{};
merged.reserve(first.size() + second.size());

size_t i = 0, j = 0;
while (i < first.size() && j < second.size()) {
    if (first[i] > second[j]) {
        merged.push_back(second[j++]);
    } else {
        merged.push_back(first[i++]);
    }
}