-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB_Array_merging.cpp
More file actions
58 lines (51 loc) · 1.96 KB
/
B_Array_merging.cpp
File metadata and controls
58 lines (51 loc) · 1.96 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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
const char nl = '\n';
// Author oGhostyyy
/* Thinking: 2 arrays a and b , merge and form c */
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt; cin >> tt;
while(tt--){
ll n; //length of a and b
cin >> n;
vector<ll> a(n); for(auto &it : a) cin >> it;
vector<ll> b(n); for(auto &it : b) cin >> it;
//input taken, looking for an O(n) ish solution
//solution should actually be fidning longest contigous group of
unordered_map<int,int> freqA, freqB;
// Count contiguous segments in array A
int currentLen = 1;
for(int i = 1; i < n; i++){
if(a[i] == a[i-1]) {
currentLen++;
} else {
freqA[a[i-1]] = max(freqA[a[i-1]], currentLen);
currentLen = 1; //there can be diff sub sequences u want the biggest
}
}
freqA[a[n-1]] = max(freqA[a[n-1]], currentLen); // Handle last segment
// Count contiguous segments in array B
currentLen = 1;
for(int i = 1; i < n; i++){
if(b[i] == b[i-1]) {
currentLen++;
} else {
freqB[b[i-1]] = max(freqB[b[i-1]], currentLen);
currentLen = 1;
}
}
freqB[b[n-1]] = max(freqB[b[n-1]], currentLen); // Handle last segment
// Find maximum combined length
int maxLen = 0;
set<int> allElements;
for(int x : a) allElements.insert(x);
for(int x : b) allElements.insert(x);
for(int element : allElements) {
maxLen = max(maxLen, freqA[element] + freqB[element]);
}
cout << maxLen << nl;
}
}