#C400. Minimum Operations to Achieve Sum \(k\)
Minimum Operations to Achieve Sum \(k\)
Minimum Operations to Achieve Sum (k)
You are given two integer arrays a
and b
of sizes n and m respectively, and a target integer k
. Your task is to determine whether there exists an element from a
and an element from b
such that their sum is exactly k
.
If such a pair exists, output 1; otherwise, output -1.
In mathematical terms, you are to check if there exist indices \( i \) and \( j \) such that:
\[ a[i] + b[j] = k \]If the condition holds, the minimum number of operations required is \(1\) (since you just need to pick the two numbers), otherwise, it is \(-1\).
inputFormat
The input is given via standard input and has the following format:
T n m k a[0] a[1] ... a[n-1] b[0] b[1] ... b[m-1] ... (repeated T times)
Where:
- T is the number of test cases.
- For each test case, the first line contains three integers: n (the size of array
a
), m (the size of arrayb
), andk
(the target sum). - The second line contains n space separated integers denoting array
a
. - The third line contains m space separated integers denoting array
b
.
outputFormat
For each test case, output a single line containing the integer 1
if there exists a pair \((a[i], b[j])\) such that \(a[i] + b[j] = k\), otherwise output -1
.
1
3 4 10
1 2 3
7 8 9 10
1
</p>