-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_3.cpp
More file actions
58 lines (44 loc) · 1.18 KB
/
6_3.cpp
File metadata and controls
58 lines (44 loc) · 1.18 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;
int main() {
int m, n;
cout << "Enter the size of the first sorted array: ";
cin >> m;
cout << "Enter the size of the second sorted array: ";
cin >> n;
int* arr1 = new int[m];
int* arr2 = new int[n];
cout << "Enter " << m << " sorted elements for the first array: ";
for (int i = 0; i < m; ++i) {
cin >> arr1[i];
}
cout << "Enter " << n << " sorted elements for the second array: ";
for (int i = 0; i < n; ++i) {
cin >> arr2[i];
}
int* merged = new int[m + n];
int i = 0, j = 0, k = 0;
while (i < m && j < n) {
if (arr1[i] < arr2[j]) {
merged[k++] = arr1[i++];
} else {
merged[k++] = arr2[j++];
}
}
while (i < m) {
merged[k++] = arr1[i++];
}
while (j < n) {
merged[k++] = arr2[j++];
}
cout << "Merged sorted array: ";
for (int idx = 0; idx < m + n; ++idx) {
cout << merged[idx] << " ";
}
cout << endl;
delete[] arr1;
delete[] arr2;
delete[] merged;
cout<<"24CE052_Pushti";
return 0;
}