Number of Adjacent Elements With the Same Color


10 Prerequisite LeetCode Problems
For “2672. Number of Adjacent Elements With the Same Color”, the following are a good preparation:
“27. Remove Element”  This problem teaches how to manipulate arrays based on certain conditions, a skill that is necessary for the current problem.
“485. Max Consecutive Ones”  This problem deals with finding consecutive elements in an array which is directly related to the current problem which requires finding adjacent elements with the same color.
“283. Move Zeroes”  This problem provides practice for operations involving moving or shifting elements in an array.
“561. Array Partition I”  This problem introduces the concept of pairings in an array which can be useful for understanding adjacency in arrays.
“167. Two Sum II  Input array is sorted”  This problem introduces the concept of pairs with certain properties in an array, which can be beneficial for understanding how to deal with adjacent elements.
“414. Third Maximum Number”  This problem can be beneficial in enhancing skills to handle and manipulate array elements based on their values, useful for coloring operations in the current problem.
“217. Contains Duplicate”  This problem is about finding duplicates in an array, understanding this can help to grasp the concept of checking for identical adjacent elements in the current problem.
“448. Find All Numbers Disappeared in an Array”  This problem can enhance the understanding of working with array indices and their values, relevant when considering indices of color operations in the current problem.
“442. Find All Duplicates in an Array”  This problem introduces handling duplicates in an array, helping you understand the comparison of adjacent elements in the current problem.
“645. Set Mismatch”  This problem is about finding the duplicate and missing number in an array. It helps you understand more about working with array values and their indices, useful when dealing with color changes at given indices.
These cover array manipulation, adjacency, and handling element properties, which are key to solving the “2672. Number of Adjacent Elements With the Same Color” problem.


There is a 0indexed array nums of length n. Initially, all elements are uncolored (has a value of 0).
You are given a 2D integer array queries where queries[i] = [indexi, colori].
For each query, you color the index indexi with the color colori in the array nums.
Return an array answer of the same length as queries where answer[i] is the number of adjacent elements with the same color after the ith query.
More formally, answer[i] is the number of indices j, such that 0 <= j < n  1 and nums[j] == nums[j + 1] and nums[j] != 0 after 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: Initially array nums = [0,0,0,0], where 0 denotes uncolored elements of the array.
 After the 1st query nums = [2,0,0,0]. The count of adjacent elements with the same color is 0.
 After the 2nd query nums = [2,2,0,0]. The count of adjacent elements with the same color is 1.
 After the 3rd query nums = [2,2,0,1]. The count of adjacent elements with the same color is 1.
 After the 4th query nums = [2,1,0,1]. The count of adjacent elements with the same color is 0.
 After the 5th query nums = [2,1,1,1]. The count of adjacent elements with the same color is 2. Example 2:
Input: n = 1, queries = [[0,100000]] Output: [0] Explanation: Initially array nums = [0], where 0 denotes uncolored elements of the array.
 After the 1st query nums = [100000]. The count of adjacent elements with the same color is 0.
Constraints:
1 <= n <= 105 1 <= queries.length <= 105 queries[i].length == 2 0 <= indexi <= n  1 1 <= colori <= 105
Problem Classification
Analysis of the Problem Statement:
Domain Categorization: This problem falls under the ‘Data Structures and Algorithms’ domain. Specifically, it deals with the manipulation and analysis of arrays.
The ‘What’ Components:
 An initial 0indexed array,
nums
, of lengthn
, where all elements are uncolored (have a value of 0).  A 2D integer array
queries
of sizem
, wherequeries[i]
=[indexi, colori]
. Each query instructs to color theindexi
with the colorcolori
in the arraynums
.  The problem asks to return an array
answer
of the same length asqueries
whereanswer[i]
is the number of adjacent elements with the same color after theith
query. The condition here is to count only those indicesj
(0 <=j
<n
 1) such thatnums[j]
==nums[j + 1]
andnums[j]
!= 0 after theith
query.
Problem Classification: This problem can be classified as a ‘Simulation’ problem. The problem simulates a set of operations (the queries) on an array, and then after each operation, it performs an evaluation (counting adjacent elements with the same color). The problem involves keeping track of states (the array after each coloring operation) and requires the use of arrays to keep track of these states and to store the queries.
Language Agnostic Coding Drills
 Concepts present in the code:
Variable Declaration: It’s one of the most basic concepts in any programming language. It’s about creating a variable and assigning a value to it. In the given code, variables like
n
,queries
,nums
,result
,c
,query
,index
,color
,pre
, andnex
are declared.List Initialization: In Python, list initialization is achieved using square brackets
[]
. In this code,nums
is initialized to a list of zeroes andresult
is an empty list.Loops: In the code, a
for
loop is used to iterate over thequeries
list. Each iteration deconstructs a query intoindex
andcolor
.Conditional Statements:
if
conditions are used in the code to check for specific conditions and run a piece of code only if that condition is true. For example, it’s used to check whether an index is within the bounds of a list before accessing the list element.List indexing: Python lists are zeroindexed. In the code, indexing is used to access and update elements in the
nums
list.Ternary Operator: The code uses ternary operators to assign a value to
pre
andnex
based on whetherindex
is within the bounds ofnums
.Arithmetic Operations: The code performs arithmetic operations like addition and subtraction to update the variable
c
.List appending: Python list method
append()
is used to add an item to the end of the list. In this code, it’s used to add the value ofc
toresult
in each iteration.
 Concepts in order of increasing difficulty:
Variable Declaration: The easiest concept, as it only requires knowing how to assign a value to a variable.
List Initialization: Easy, but requires understanding of the list data structure.
Arithmetic Operations: Basic arithmetic operations like addition and subtraction.
List indexing: Requires understanding of how indexing works in Python.
Conditional Statements: Requires logical thinking to construct the correct condition.
Loops: Requires understanding of how loops work and how to use them to iterate over a data structure.
Ternary Operator: Requires understanding of conditional expressions in Python.
List appending: Intermediate concept, involves adding items to a list in correct order.
 Problemsolving approach:
The solution to the problem involves iterating through each query and updating the nums
list based on the index
and color
in the query.
The code maintains a counter c
to keep track of the number of adjacent elements with the same color in nums
. The counter is updated based on whether the color of nums[index]
is equal to the color of its previous and next element.
After processing each query, the current value of c
is appended to the result
list. At the end of the loop, result
will contain the number of adjacent elements with the same color after each query.
Targeted Drills in Python
 Pythonbased coding drills for each identified concept:
 Variable Declaration:


 List Initialization:


 Arithmetic Operations:


 List indexing:


 Conditional Statements:


 Loops:


 Ternary Operator:


 List appending:


 Problemspecific concept:
 Iterating over a list of queries and updating a list based on each query. This is essential for our problem as the solution involves processing a list of queries and updating the
nums
list based on each query. Here is a basic coding drill for this concept:


 To solve the initial problem, these drills can be integrated together in the following order:
Initialize the
nums
list and theresult
list.Iterate over the
queries
list.In each iteration, extract
index
andcolor
from the current query.Check the color of the previous and next element of
nums[index]
and update the counterc
.Update
nums[index]
withcolor
and again check the color of the previous and next element and updatec
.Append the current value of
c
toresult
.At the end of the loop,
result
will contain the answer to our problem.