Mobile coding works. A laptop is faster for long sessions.
Problem
Two arrays of integers `a` and `b` are given, both sorted in
non-decreasing order. Determine how many pairs `(i, j)` can be formed such
that `a[i] == b[j]`, following this rule: traverse both arrays simultaneously
with two indices, and each element may be used at most once
in a pair.
In other words, once `a[i]` is paired with `b[j]`, both indices
advance and cannot be reused. This is exactly the
pair-counting variant of the merge algorithm.
Input:
The program reads from standard input:
- a natural number `n`, followed by `n` integers (array `a`);
- a natural number `m`, followed by `m` integers (array `b`).
- `1 <= n, m <= 1000`
- `-1000000 <= values <= 1000000`
- the arrays are sorted in non-decreasing order and may contain duplicates
Output:
The program prints to standard output a single non-negative integer: the total number of
equal pairs formed, followed by a newline.
Example:
Input:
5
1 2 3 4 5
3
2 4 6
Output:
2
Stuck?
Use the Get Hint button in the action bar to reveal a guided hint.
💬 Discussion
Loading...💬
No comments yet. Be the first to start the discussion!
💻 No output yet.
Click Run in the editor to execute your code with the custom input.
Solve it your way first
Community solutions unlock after you submit a passing solution. Don't peek.
Ready to solve this challenge?
Create a free account to write code, submit solutions, and track your progress.
⌨️ Keyboard Shortcuts
Code Editor
Run Code
Ctrl
Enter
Submit Code
Ctrl
Shift
Enter
Format Code
Shift
Alt
F
Toggle Comment
Ctrl
/
Undo
Ctrl
Z
Redo
Ctrl
Y
Navigation
Global Search
/
Show Shortcuts
?
Close Modal
Esc