#K52522. Count Valid Pairs
Count Valid Pairs
Count Valid Pairs
You are given two arrays (A) and (B) each of length (N) and an integer (X). A pair is considered valid if there exists an index (i) in (A) and an index (j) in (B) such that
[
A[i] + B[j] = X
]
Your task is to count the number of indices (i) in array (A) for which there exists an element in (B) making the sum equal to (X). Note that even if there are multiple matching elements in (B), you should only count each (A[i]) at most once.
Example:
If (N = 3), (X = 7), (A = [1,2,3]) and (B = [6,5,4]), then there are 3 valid pairs because (1+6=7), (2+5=7) and (3+4=7).
inputFormat
The input is given via standard input (stdin) and consists of three lines:
- The first line contains two integers \(N\) and \(X\), where \(N\) is the length of the arrays and \(X\) is the target sum.
- The second line contains \(N\) integers representing the array \(A\), separated by spaces.
- The third line contains \(N\) integers representing the array \(B\), separated by spaces.
outputFormat
Output a single integer to standard output (stdout): the count of valid indices (i) from array (A) for which there exists an element in (B) such that (A[i] + B[j] = X).## sample
3 7
1 2 3
6 5 4
3
</p>