You are building firmware for a seismograph's onboard logger. Signed amplitude readings arrive one at a time and are permanently appended to the instrument's register (nothing is ever removed, and a reading can repeat). Periodically the analysis software issues a query: given a target value, is there a way to pick two distinct logged entries (two separate readings taken at possibly the same or different times) whose amplitudes add up exactly to that target? Note that if a value has only been logged once so far, it cannot be paired with itself — a second, independent logging of that same value is required to use it twice in a sum.
Process a sequence of operations in order. Each operation is either LOG v (append reading v to the register) or QUERY x (ask whether two distinct entries currently in the register sum to x).
n, the number of operations.n lines each contain either LOG v or QUERY x.For every QUERY operation, in the order encountered, print 1 if some two distinct logged entries sum exactly to the queried value, otherwise print 0. Print one result per line; do not print anything for LOG operations.
LOGQUERYExample 1
Input
5 LOG 1 LOG 3 LOG 5 QUERY 4 QUERY 7
Expected
1 0
Explanation
After logging 1, 3, and 5, the possible pair sums are 1+3=4, 1+5=6, and 3+5=8. QUERY 4 matches the pair (1,3), so the answer is 1. QUERY 7 matches none of {4,6,8}, so the answer is 0.
Example 2
Input
4 LOG 2 QUERY 4 LOG 2 QUERY 4
Expected
0 1
Explanation
After the first LOG 2, only a single entry (value 2) exists, so QUERY 4 would need to pair that entry with itself, which is not allowed -- the answer is 0. After the second LOG 2, there are two distinct logged entries both equal to 2, so 2+2=4 is now a valid pair and QUERY 4 answers 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →