Untitled

mail@pastecode.io avatar
unknown
c_cpp
13 days ago
667 B
3
Indexable
Never
#include <vector>
using namespace std;

vector<int> mergeArrays(const vector<int>& a, const vector<int>& b) {
    vector<int> result;
    size_t i = 0, j = 0;
    
    while (i < a.size() && j < b.size()) {
        if (a[i] <= b[j]) {
            result.push_back(a[i]);
            i++;
        } else {
            result.push_back(b[j]);
            j++;
        }
    }
    
    // Add remaining elements from array a, if any
    while (i < a.size()) {
        result.push_back(a[i]);
        i++;
    }
    
    // Add remaining elements from array b, if any
    while (j < b.size()) {
        result.push_back(b[j]);
        j++;
    }
    
    return result;
}
Leave a Comment