#K8086. Frequency Tracker
Frequency Tracker
Frequency Tracker
You are required to design a data structure named FrequencyTracker
that supports three operations:
- Add:
add(number)
addsnumber
to the data structure. - Remove:
remove(number)
removes one occurrence ofnumber
from the data structure if it exists. - Query:
hasFrequency(frequency)
checks whether there exists any number in the data structure with exactly the specified frequency. It returnstrue
if such a number exists andfalse
otherwise.
You will be given a sequence of operations through the standard input. Each operation is represented as a pair of integers. The first integer is the operation code, and the second is the argument:
- 1: add
number
- 2: remove
number
- 3: query if any number appears exactly
frequency
times
For each query operation, output the result (true
or false
) on a separate line to the standard output.
All formulas are rendered in LaTeX format when referring to mathematical expressions. For instance, an operation count is denoted by \(Q\), and the frequency by \(f\).
inputFormat
The input is given via standard input, and it has the following format:
- The first line contains an integer \(Q\) denoting the number of operations.
- Each of the next \(Q\) lines contains two integers separated by space: the first one is the operation code and the second one is the argument.
Operation codes:
1 x
: Add the number \(x\) to the data structure.2 x
: Remove one occurrence of the number \(x\) (if present).3 f
: Query whether there is any number with frequency exactly \(f\). For this operation, you should outputtrue
orfalse
on a separate line.
outputFormat
For each query operation (operation code 3), print a single line containing either true
or false
to the standard output.
6
1 3
1 3
3 2
2 3
3 2
3 1
true
false
true
</p>