Problem · Hash Table

Array Manipulation

Learn this problem
MediumDatabricksFULLTIMEOA

Problem statement

Start with an empty multiset of integers called numbers. Process the queries in order:

  • +x appends one occurrence of x. Duplicate values are allowed.
  • -x removes every occurrence of x. 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

arrayManipulation(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

🐢

More Databricks problems

drafts saved locally
public int[] arrayManipulation(String[] queries, int diff) {
  // write your code here
}
queries["+4", "+5", "+6", "+4", "+3", "-4"]
diff1
expected[0, 0, 1, 2, 4, 0]
checking account