Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer n representing an array colors of length n where all elements are set to 0's meaning uncolored. You are also given a 2D integer array queries where queries[i] = [indexi, colori]. For the ith query:
colors[indexi] to colori.colors which have the same color (regardless of colori).Return an array answer of the same length as queries where answer[i] is the answer to the ith query.
Example 1:
Input: n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]
Output: [0,1,1,0,2]
Explanation:
Example 2:
Input: n = 1, queries = [[0,100000]]
Output: [0]
Explanation:
After the 1st query colors = [100000]. The count of adjacent pairs with the same color is 0.
Constraints:
1 <= n <= 1051 <= queries.length <= 105queries[i].length == 20 <= indexi <= n - 11 <= colori <= 105Problem summary: You are given an integer n representing an array colors of length n where all elements are set to 0's meaning uncolored. You are also given a 2D integer array queries where queries[i] = [indexi, colori]. For the ith query: Set colors[indexi] to colori. Count the number of adjacent pairs in colors which have the same color (regardless of colori). Return an array answer of the same length as queries where answer[i] is the answer to the ith query.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
4 [[0,2],[1,2],[3,1],[1,1],[2,1]]
1 [[0,100000]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2672: Number of Adjacent Elements With the Same Color
class Solution {
public int[] colorTheArray(int n, int[][] queries) {
int m = queries.length;
int[] nums = new int[n];
int[] ans = new int[m];
for (int k = 0, x = 0; k < m; ++k) {
int i = queries[k][0], c = queries[k][1];
if (i > 0 && nums[i] > 0 && nums[i - 1] == nums[i]) {
--x;
}
if (i < n - 1 && nums[i] > 0 && nums[i + 1] == nums[i]) {
--x;
}
if (i > 0 && nums[i - 1] == c) {
++x;
}
if (i < n - 1 && nums[i + 1] == c) {
++x;
}
ans[k] = x;
nums[i] = c;
}
return ans;
}
}
// Accepted solution for LeetCode #2672: Number of Adjacent Elements With the Same Color
func colorTheArray(n int, queries [][]int) (ans []int) {
nums := make([]int, n)
x := 0
for _, q := range queries {
i, c := q[0], q[1]
if i > 0 && nums[i] > 0 && nums[i-1] == nums[i] {
x--
}
if i < n-1 && nums[i] > 0 && nums[i+1] == nums[i] {
x--
}
if i > 0 && nums[i-1] == c {
x++
}
if i < n-1 && nums[i+1] == c {
x++
}
ans = append(ans, x)
nums[i] = c
}
return
}
# Accepted solution for LeetCode #2672: Number of Adjacent Elements With the Same Color
class Solution:
def colorTheArray(self, n: int, queries: List[List[int]]) -> List[int]:
nums = [0] * n
ans = [0] * len(queries)
x = 0
for k, (i, c) in enumerate(queries):
if i > 0 and nums[i] and nums[i - 1] == nums[i]:
x -= 1
if i < n - 1 and nums[i] and nums[i + 1] == nums[i]:
x -= 1
if i > 0 and nums[i - 1] == c:
x += 1
if i < n - 1 and nums[i + 1] == c:
x += 1
ans[k] = x
nums[i] = c
return ans
// Accepted solution for LeetCode #2672: Number of Adjacent Elements With the Same Color
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2672: Number of Adjacent Elements With the Same Color
// class Solution {
// public int[] colorTheArray(int n, int[][] queries) {
// int m = queries.length;
// int[] nums = new int[n];
// int[] ans = new int[m];
// for (int k = 0, x = 0; k < m; ++k) {
// int i = queries[k][0], c = queries[k][1];
// if (i > 0 && nums[i] > 0 && nums[i - 1] == nums[i]) {
// --x;
// }
// if (i < n - 1 && nums[i] > 0 && nums[i + 1] == nums[i]) {
// --x;
// }
// if (i > 0 && nums[i - 1] == c) {
// ++x;
// }
// if (i < n - 1 && nums[i + 1] == c) {
// ++x;
// }
// ans[k] = x;
// nums[i] = c;
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2672: Number of Adjacent Elements With the Same Color
function colorTheArray(n: number, queries: number[][]): number[] {
const nums: number[] = new Array(n).fill(0);
const ans: number[] = [];
let x = 0;
for (const [i, c] of queries) {
if (i > 0 && nums[i] > 0 && nums[i - 1] == nums[i]) {
--x;
}
if (i < n - 1 && nums[i] > 0 && nums[i + 1] == nums[i]) {
--x;
}
if (i > 0 && nums[i - 1] == c) {
++x;
}
if (i < n - 1 && nums[i + 1] == c) {
++x;
}
ans.push(x);
nums[i] = c;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.