Java Solution Beat 100% with Single Loop 💯✅
Intuition1. Initialization:
We need to maintain two lists:

seen: This will store the elements we have encountered so far in the array, specifically the positiv
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
**1. Initialization:**
- We need to maintain two lists:
    - seen: This will store the elements we have encountered so far in the array, specifically the positive integers, with the most recently encountered elements at the front.
    - ans: This will store the result for each negative integer in the nums array.
- Two helper variables:
    - k: This tracks the number of valid steps or interactions for the current negative integer.
    - prev: This keeps track of the previous negative integer encountered to help decide how the result should evolve.

**2. Iterate Through the Array:**
- Positive Integer Case: If the current element is positive:
    - Add it to the front of the seen list.
    - Reset the helper variables prev and k.
- Negative Integer Case: If the current element is negative:
    - Check the previous state (prev).
    - Update k accordingly:
        - If prev is 0, initialize the counter (k++).
        - If the previous integer was the same as the current one, increment k.
        - Otherwise, reset k and prev.
    - The result for this negative number is determined by the value of k. If k exceeds the number of elements in seen, return -1. Otherwise, retrieve the k-th most recent seen element.

**3. Return the Result:**
- The list ans will store the answers for each negative number encountered during the iteration.

# Approach
<!-- Describe your approach to solving the problem. -->
**1. Initialization:**
- We need to maintain two lists:
    - seen: This will store the elements we have encountered so far in the array, specifically the positive integers, with the most recently encountered elements at the front.
    - ans: This will store the result for each negative integer in the nums array.
- Two helper variables:
    - k: This tracks the number of valid steps or interactions for the current negative integer.
    - prev: This keeps track of the previous negative integer encountered to help decide how the result should evolve.

**2. Iterate Through the Array:**
- Positive Integer Case: If the current element is positive:
    - Add it to the front of the seen list.
    - Reset the helper variables prev and k.
- Negative Integer Case: If the current element is negative:
    - Check the previous state (prev).
    - Update k accordingly:
        - If prev is 0, initialize the counter (k++).
        - If the previous integer was the same as the current one, increment k.
        - Otherwise, reset k and prev.
    - The result for this negative number is determined by the value of k. If k exceeds the number of elements in seen, return -1. Otherwise, retrieve the k-th most recent seen element.

**3. Return the Result:**
- The list ans will store the answers for each negative number encountered during the iteration.

# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public List<Integer> lastVisitedIntegers(int[] nums) {
        List<Integer> seen = new ArrayList<>();
        List<Integer> ans = new ArrayList<>();
        int k = 0;
        int prev = 0;
        for (int el : nums) {
            if (el > 0) {
                seen.add(0, el);
                prev = 0;
                k = 0;
            } else {
                if (prev == 0) {
                    k++;
                    prev = -1;
                } else if (prev == el) {
                    k++;
                } else {
                    k = 0;
                    prev = 0;
                }
                if (k > seen.size()) {
                    ans.add(-1);
                } else {
                    ans.add(seen.get(k - 1));
                }
            }
        }
        return ans;
    }
}
```
['Java']