cswork / OAOpen practice workspace

DatabricksOA #4

Triples with Common Difference (Dynamic Multiset)

Estimated MediumStatement and editorial — judging is being prepared

Topics (AI-labelled)Hashing

Given an empty array that should contain integer numbers, your task is to process a list of queries on it. Specifically, there are two types of queries:

  • "+x" — append x to numbers. numbers may contain multiple instances of the same number.
  • "-x" — remove all the instances of x from numbers.

After processing each query, count the number of triples (x, y, z) in numbers which meet this condition: both x - y and y - z are equal to a given diff. The final output should be an array of counts after each query.

Example: queries=["+4","+5","+6","+4","+3","-4"], diff=1 returns [0, 0, 1, 2, 4, 0].

Constraints

values in [-10⁹, 10⁹]; at most 10⁵ queries.

Editorial

Loading solution…