-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGFG_UnionOfArray.cpp
More file actions
executable file
·79 lines (73 loc) · 1.16 KB
/
GFG_UnionOfArray.cpp
File metadata and controls
executable file
·79 lines (73 loc) · 1.16 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lli long long int
#define vi vector<int>
#define forinput for(int i = 0; i<n ; i++){cin>>arr1[i];}
#define forinput2 for(int i = 0; i<m ; i++){cin>>arr2[i];}
#define foroutput for(int i = 0; i<n; i++){cout<<arr[i]<<" ";}
#define pb push_back
#define pob pop_back
void incSort(int arr[], int n){
for(int i = 0; i < n ; i++){
for(int j = i+1; j < n; j++){
if(arr[i]>arr[j]){
swap(arr[i], arr[j]);
}
}
}
}
void arrUnion(int arr1[], int arr2[], int n, int m){
int i = 0, j = 0, count = 0;
while(i < n && j < m){
if(arr1[i] < arr2[j]){
i++;
count++;
}
else if(arr1[i] > arr2[j]){
j++;
count++;
}
else{
i++;
j++;
count++;
}
}
while(i < n){
i++;
count++;
}
while(j < m){
j++;
count++;
}
cout<<count<<endl;
}
int main(){
int t; cin>>t;
while(t--){
int n; cin>>n;
int m; cin>>m;
int arr1[n];
int arr2[m];
forinput
forinput2
for(int i = 0; i < n; i++)
{
if(arr1[i]>arr1[i+1]){
incSort(arr1, n);
break;
}
}
for(int i = 0; i < m; i++)
{
if(arr2[i] > arr2[i+1]){
incSort(arr2, m);
break;
}
}
arrUnion(arr1, arr2, n, m);
}
return 0;
}