We iterate from a nested-loop pair check to a single-pass scan that only remembers the most recent 1 and 2. The "last-seen pointer" pattern crops up everywhere once you spot it.
We iterate from a nested-loop pair check to a single-pass scan that only remembers the most recent 1 and 2. The "last-seen pointer" pattern crops up everywhere once you spot it.
You are given an integer array nums consisting only of 0, 1, and 2.
A pair of indices (i, j) is called valid if nums[i] == 1 and nums[j] == 2.
Return the minimum absolute difference between i and j among all valid pairs. If no valid pair exists, return -1.
The absolute difference between indices i and j is defined as abs(i - j).