Master essential array techniques including two pointers, sliding window, and prefix sums for efficient problem solving with O(n) patterns.
Provides O(n) array patterns: two pointers, sliding window, and prefix sums for efficient range queries and subarray operations.
/plugin marketplace add pluginagentmarketplace/custom-plugin-data-structures-algorithms/plugin install data-structures-algorithms-assistant@pluginagentmarketplace-data-structures-algorithmsThis skill inherits all available tools. When active, it can use any tool Claude has access to.
assets/arrays_config.yamlprefix-sum.mdreferences/ARRAYS_GUIDE.mdscripts/array_ops.pysliding-window.mdAtomic Responsibility: Execute array manipulation patterns with optimal complexity.
from typing import List
def remove_element(nums: List[int], val: int) -> int:
"""
Remove all occurrences of val in-place.
Time: O(n), Space: O(1)
Args:
nums: Input array (modified in-place)
val: Value to remove
Returns:
New length of array
Raises:
ValueError: If nums is None
"""
if nums is None:
raise ValueError("Input array cannot be None")
write_ptr = 0
for read_ptr in range(len(nums)):
if nums[read_ptr] != val:
nums[write_ptr] = nums[read_ptr]
write_ptr += 1
return write_ptr
def two_sum_sorted(arr: List[int], target: int) -> List[int]:
"""
Find two indices in sorted array that sum to target.
Time: O(n), Space: O(1)
Args:
arr: Sorted input array
target: Target sum
Returns:
List of two indices, or empty list if not found
"""
if not arr or len(arr) < 2:
return []
left, right = 0, len(arr) - 1
while left < right:
total = arr[left] + arr[right]
if total == target:
return [left, right]
elif total < target:
left += 1
else:
right -= 1
return []
def max_sum_subarray(nums: List[int], k: int) -> int:
"""
Find maximum sum of contiguous subarray of size k.
Time: O(n), Space: O(1)
Args:
nums: Input array
k: Window size
Returns:
Maximum sum, or 0 if invalid input
Raises:
ValueError: If k > len(nums) or k <= 0
"""
if not nums or k <= 0:
raise ValueError("Invalid input: empty array or k <= 0")
if k > len(nums):
raise ValueError(f"Window size {k} exceeds array length {len(nums)}")
window_sum = sum(nums[:k])
max_sum = window_sum
for i in range(k, len(nums)):
window_sum = window_sum - nums[i - k] + nums[i]
max_sum = max(max_sum, window_sum)
return max_sum
def min_subarray_len(target: int, nums: List[int]) -> int:
"""
Find minimal length of subarray with sum >= target.
Time: O(n), Space: O(1)
Args:
target: Minimum sum required
nums: Input array of positive integers
Returns:
Minimum length, or 0 if no valid subarray
"""
if not nums:
return 0
left = 0
current_sum = 0
min_len = float('inf')
for right in range(len(nums)):
current_sum += nums[right]
while current_sum >= target:
min_len = min(min_len, right - left + 1)
current_sum -= nums[left]
left += 1
return min_len if min_len != float('inf') else 0
class PrefixSum:
"""
Precompute cumulative sums for O(1) range queries.
Time: O(n) initialization, O(1) query
Space: O(n)
"""
def __init__(self, nums: List[int]):
if nums is None:
raise ValueError("Input array cannot be None")
self.prefix = [0]
for num in nums:
self.prefix.append(self.prefix[-1] + num)
def range_sum(self, left: int, right: int) -> int:
"""Get sum of elements from left to right (inclusive)."""
if left < 0 or right >= len(self.prefix) - 1:
raise IndexError(f"Indices out of bounds: [{left}, {right}]")
return self.prefix[right + 1] - self.prefix[left]
import pytest
class TestArrayTechniques:
def test_two_sum_sorted_found(self):
assert two_sum_sorted([1, 2, 3, 4, 5], 9) == [3, 4]
def test_two_sum_sorted_not_found(self):
assert two_sum_sorted([1, 2, 3], 10) == []
def test_sliding_window_basic(self):
assert max_sum_subarray([1, 2, 3, 4, 5], 2) == 9
def test_prefix_sum_range(self):
ps = PrefixSum([1, 2, 3, 4, 5])
assert ps.range_sum(1, 3) == 9
def test_min_subarray_len(self):
assert min_subarray_len(7, [2, 3, 1, 2, 4, 3]) == 2
| Issue | Cause | Solution |
|---|---|---|
| Off-by-one error | Wrong index bounds | Use len(arr) - 1 for last valid index |
| Infinite loop | Window never shrinks | Ensure left pointer advances |
| Wrong sum | Missing edge case | Handle empty array, single element |
□ Input validation (None, empty, invalid k)?
□ Pointer bounds checked (left < right)?
□ Window sum updated correctly?
□ Edge cases: single element, all same values?
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.