length array of size n + 2 to track the length of groups, and a count array of size n + 1 to track the count of groups of each length.a in the array arr.a, find the length of the group to the left (length[a - 1]) and to the right (length[a + 1]).length array at positions a, a - left, and a + right to the new group length left + right + 1.count array by decrementing the counts for left and right and incrementing the count for the new group length.count[m] is greater than 0, update the result to the current step index i + 1.m was found; otherwise, return -1.