Track Target Occurrence Patterns Across Array Regions
Arrays & HashingAuto-graded · 19 test casesLast updated
Identifies whether a target value occurs at even/odd indices and in the first/second half of an even-length array in one pass.
Problem Statement
Given an array nums of integers with even length and a target integer target, determine whether target appears in four distinct categories within the array:
- Even-indexed position: Does
targetoccur at any index wherei % 2 == 0? - Odd-indexed position: Does
targetoccur at any index wherei % 2 == 1? - First half: Does
targetoccur in the first half of the array (indices0throughn//2 - 1)? - Second half: Does
targetoccur in the second half of the array (indicesn//2throughn - 1)?
A single occurrence of target can satisfy multiple categories simultaneously. For instance, if target appears at index 0, it satisfies both the even-indexed and first-half categories.
Return a list of four boolean values [even_index, odd_index, first_half, second_half] indicating whether each category contains at least one occurrence of target.
Implement an efficient solution that scans the array once from left to right and stops early if all four categories are satisfied before reaching the end.
Constraints
2 <= len(nums) <= 10^5len(nums)is always even-10^9 <= nums[i], target <= 10^9
Examples
Example 1
Input: track_target_regions([4, 2, 4, 7, 1, 4], 4)
Output: [True, True, True, True]
Example 2
Input: track_target_regions([1, 3, 5, 7], 3)
Output: [False, True, True, False]
Hints
Hint 1
Each occurrence of target updates exactly one parity flag (even or odd index) and one half flag (first or second half).
Hint 2
Use four boolean flags and update them during a single pass through the array.
Hint 3
Once all four flags become True, you can terminate early since no future element will change the result.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem asks us to track whether a target value appears in four independent categories. Each array position contributes to exactly two categories: one based on index parity (even or odd) and one based on array half (first or second).
We maintain four boolean flags initialized to False. During a single left-to-right pass, whenever we encounter the target value, we set the appropriate flags based on the current index. Since each index has a defined parity and belongs to exactly one half, we update one flag from each pair.
The key optimization is early termination: once all four flags are True, no subsequent element can change the result, so we can stop scanning immediately.
Why This Works
Correctness: Each flag transitions from False to True only when we actually observe an index matching both the target value and that flag's condition. The flags never revert to False, so once set, they remain accurate through the end of the scan.
Completeness: If any category should be True, we will encounter at least one matching index during our scan (either we reach it naturally, or we've already covered all categories and terminated early).
Early exit safety: The flags are monotonic—they only change in one direction. Once all four are True, the output is fixed regardless of remaining elements.
Complexity
Time: O(n) worst case, occurring when the target is absent or some category is never satisfied. Best case can be much faster with early termination when all categories are quickly satisfied.
Space: O(1) auxiliary space, using only four boolean flags and a few index variables.
Implementation
from typing import List
def track_target_regions(nums: List[int], target: int) -> List[bool]:
n = len(nums)
result = [False, False, False, False] # [even_index, odd_index, first_half, second_half]
midpoint = n // 2
for i, val in enumerate(nums):
if val == target:
# Update parity flag
if i % 2 == 0:
result[0] = True
else:
result[1] = True
# Update half flag
if i < midpoint:
result[2] = True
else:
result[3] = True
# Early exit if all categories are satisfied
if all(result):
break
return result