Problem · Hash Table
Count Triples with Difference
Learn this problemProblem statement
Start with an empty multiset of integers called numbers. Process the queries in order:
+xappends one occurrence ofx. Duplicate values are allowed.-xremoves every occurrence ofx. The value is guaranteed to be present.
After each query, count the triples of element occurrences (x, y, z) that can be rearranged so that x - y = y - z = diff. Return all counts in query order.
Function
databricksCountTriplesWithDifference(queries: String[], diff: int) → int[]Examples
Example 1
queries = ["+4", "+5", "+6", "+4", "+3", "-4"]diff = 1return = [0, 0, 1, 2, 4, 0]After adding 6, the triple is (6,5,4). The second 4 doubles that count. Adding 3 also creates two copies of (5,4,3). Removing 4 removes all such triples.
Constraints
🐥🧡🐝