Arithmetic Triplets After Queries
Problem statement
You are given an ordered array queries and a positive integer diff. Initially, a multiset of integers is empty.
+xadds one occurrence of integerx.-xremoves all occurrences of integerx. Removing an absent value changes nothing.
After each query, count the triples of occurrences whose values x, y, z satisfy x - y = y - z = diff. Each choice of occurrences counts separately, so a value triple contributes the product of its three frequencies. Return these counts in query order.
For this exercise, assume the first character of each query is the operation, and the remaining characters are a signed decimal integer with no spaces. For example, +-2 inserts negative two and --2 removes all negative twos. Counts use 64-bit integers.
Function
countArithmeticTriplets(queries: String[], diff: int) → long[]Examples
Example 1
queries = ["+1","+3","+5","+5"]diff = 2return = [0,0,1,2]The third query forms one triple with values 5, 3, 1. The fourth adds a second occurrence of 5, creating two choices for that occurrence.
Constraints
- For this exercise, assume
0 <= queries.length <= 10^5. - For this exercise, assume
-10^8 <= x <= 10^8and1 <= diff <= 10^8. - Every query has a valid operation followed by a valid signed decimal integer.
- Use a signed 64-bit integer for each returned count.