Number of Adjacent Elements With the Same Color - Problem
You are given an integer n representing an array colors of length n where all elements are initially set to 0 (uncolored).
You are also given a 2D integer array queries where queries[i] = [indexi, colori].
For each query:
- Set
colors[indexi]tocolori - Count the number of adjacent pairs in the array that have the same color
Return an array answer where answer[i] is the count of same-colored adjacent pairs after the i-th query.
Input & Output
Example 1 — Basic Color Updates
$
Input:
n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]
›
Output:
[0,1,1,0,2]
💡 Note:
Initially [0,0,0,0]. After [0,2]: [2,0,0,0] → 0 pairs. After [1,2]: [2,2,0,0] → 1 pair (2,2). After [3,1]: [2,2,0,1] → 1 pair. After [1,1]: [2,1,0,1] → 0 pairs. After [2,1]: [2,1,1,1] → 2 pairs.
Example 2 — Single Element
$
Input:
n = 1, queries = [[0,1]]
›
Output:
[0]
💡 Note:
Array [1] has no adjacent pairs, so count is 0.
Example 3 — No Adjacent Matches
$
Input:
n = 3, queries = [[0,1],[1,2],[2,3]]
›
Output:
[0,0,0]
💡 Note:
Array becomes [1,2,3] with no adjacent elements having the same color.
Constraints
- 1 ≤ n ≤ 105
- 1 ≤ queries.length ≤ 105
- queries[i].length == 2
- 0 ≤ indexi < n
- 1 ≤ colori ≤ 105
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code