-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeArrays.cpp
More file actions
43 lines (32 loc) · 873 Bytes
/
mergeArrays.cpp
File metadata and controls
43 lines (32 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <vector>
#include <iostream>
using namespace std;
/*
* Complete the function below.
*/
vector < int > mergeArrays(vector < int > a, vector < int > b) {
vector<int> output;
//Pointers to indices of a & b respectively
int i=0, j = 0;
while (i < a.size() && j < b.size()) {
cout<<i<<" "<<j<<endl;
if (a[i] < b[j]) {
output.push_back(a[i]); i++;
} else {
output.push_back(b[j]); j++;
}
}
// Now push the remaining elements in a & b
while (i < a.size()) {
output.push_back(a[i++]);
}
while (j < b.size()) {
output.push_back(b[j++]);
}
return output;
}
int main() {
vector<int> v1 = {2,4,6};
vector<int> v2 = {1,3,5};
vector<int> v3 =mergeArrays(v1,v2);
}