-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04.Maximum_Common_Elements.cpp
More file actions
39 lines (38 loc) · 1.28 KB
/
Copy path04.Maximum_Common_Elements.cpp
File metadata and controls
39 lines (38 loc) · 1.28 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
//Maximum Common Elements - codechef (1100)
//https://www.codechef.com/practice/course/two-pointers/POINTERF/problems/PREP17
/*
Given two arrays A and B, each of size N, where each array consists of distinct elements.
Find the number of elements that are common in both the arrays.
Input Format
The first line of input will contain a single integer T, denoting the number of test cases.
Each test case consists of multiple lines of input.
The first line of each test case contains an integer N — the size of both the arrays.
The second line contains N space separated integers - the elements of array A.
The third line contains N space separated integers - the elements of array B.
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector <int> a(n);
vector <int> b(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int i = 0, j = 0, cnt = 0;
while (i < n && j < n) {
if (a[i] == b[j]) {
cnt++;
i++;
j++;
}
else if (a[i] != b[j]) i++;
}
cout << cnt << endl;
}
}