Integers arrive as '+x' (add one copy of x) and '-x' (remove every copy of x). After each query, count the triples of elements with values v, v+d and v+2d for a given d.
Unlock detailed company stats for this questionUpgrade
Start with an empty collection of integers, in which the same value may appear more than once, and a fixed difference d. Process a list of queries of two kinds: '+x' adds one copy of x, and '-x' removes every copy of x. After each query, count the triples of elements (x, y, z) in the collection with x - y = d and y - z = d, that is, three elements whose values are v, v + d and v + 2d. The position of the elements in the collection does not matter, and equal values are distinct elements, so if those three values occur a, b and c times they contribute a * b * c triples. Return the list of counts, one per query. All values lie between -10^9 and 10^9, a removal always names a value that is present, and every count fits in a 32-bit integer. Example with d = 5: the queries +5, +5, +0, +10, +10, -5 give 0, 0, 0, 2, 4, 0.
Community answers
No answers contributed by the community yet.
Related questions
Given an array of integers, find the next greater integer for each element in the array.Implement an integer map with insert, get, addToKey (add x to every key) and addToValue (add y to every value); apply up to 100,000 queries and return the sum of all get results.Related courses

Course

