-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticep14.cpp
More file actions
60 lines (48 loc) · 1.68 KB
/
practicep14.cpp
File metadata and controls
60 lines (48 loc) · 1.68 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
59
60
#include <iostream>
using namespace std;
// You are working on a mathematics application that requires merging two sorted lists of numbers
// into a single sorted list without any duplicates.
// Your task is to write a program that performs this merging operation on two lists of numbers.
// Your program should merge the numbers from the first and second lists into a single sorted list,
// removing any duplicate numbers. The merged list should contain all the unique numbers, arranged in ascending order.
// After merging the lists, your program should output the numbers in the merged list,
// according to the initial array.
int main() {
int n, k;
// Read the size and elements of the first list
cin >> n;
int arr1[n];
for (int i = 0; i < n; ++i) {
cin >> arr1[i];
}
// Read the size and elements of the second list
cin >> k;
int arr2[k];
for (int i = 0; i < k; ++i) {
cin >> arr2[i];
}
int i = 0, j = 0;
// Output merged elements in sorted order without duplicates
while (i < n && j < k) {
if (arr1[i] < arr2[j]) {
cout << arr1[i++] << " ";
} else if (arr1[i] > arr2[j]) {
cout << arr2[j++] << " ";
} else {
cout << arr1[i] << " "; // arr1[i] == arr2[j], so print one of them
i++;
j++;
}
}
// Output remaining elements from arr1 (if any)(if one list is larger than the other )
//anyway its in sorted order so print them
while (i < n) {
cout << arr1[i++] << " ";
}
// Output remaining elements from arr2 (if any)
while (j < k) {
cout << arr2[j++] << " ";
}
cout << endl;
return 0;
}