Practice
count_nice_subarrays([2,1,2,1], 1)?Solution
Step 1: Trace at_most_k(nums, 1)
Counting subarrays with at most 1 odd: - right=0 (2): odd_count=0, count=1 - right=1 (1): odd_count=1, count=1+(1-0+1)=3 - right=2 (2): odd_count=1, count=3+(2-0+1)=6 - right=3 (1): odd_count=2 >1, shrink left until odd_count=1 left moves from 0 to 1, odd_count=1, count=6+(3-1+1)=9 Total at_most_k(1)=9Step 2: Trace at_most_k(nums, 0)
Counting subarrays with at most 0 odd: - right=0 (2): odd_count=0, count=1 - right=1 (1): odd_count=1 >0, shrink left to 1, odd_count=0, count=1+(1-1+1)=2 - right=2 (2): odd_count=0, count=2+(2-1+1)=4 - right=3 (1): odd_count=1 >0, shrink left to 3, odd_count=0, count=4+(3-3+1)=5 Total at_most_k(0)=5Final Answer:
count_nice_subarrays = 9 - 5 = 4Quick Check:
Rechecking counts confirms 4 subarrays with exactly 1 odd [OK]
- Off-by-one in counting subarrays
- Miscounting odd numbers in window
- Forgetting to shrink window correctly
Solution
Step 1: Understand the problem constraints
The problem requires finding the longest consecutive sequence in an unsorted array efficiently.Step 2: Evaluate approaches for optimality
Sorting then scanning is O(n log n), brute force scanning is O(n²), and DP is not straightforward here. Using a hash set to track numbers and only starting sequences from numbers without a predecessor ensures each sequence is counted once, achieving O(n) time.Final Answer:
Option D -> Option DQuick Check:
Hash set approach avoids redundant work and achieves linear time [OK]
- Assuming sorting is always optimal
- Trying DP without clear subproblem
- Brute force scanning entire array repeatedly
max_length after processing the input [0, 1, 0, 1]?Solution
Step 1: Trace prefix sums and first_occurrence updates.
For input [0,1,0,1], prefix sums are: -1, 0, -1, 0 with offset indexing.Step 2: Update max_length when prefix sum repeats.
At i=1, prefix_sum=0 repeats at index -1 -> length=2; at i=3, prefix_sum=0 repeats again -> length=4.Final Answer:
Option B -> Option BQuick Check:
Longest balanced subarray is entire array length 4 [OK]
- Off-by-one in length calculation
- Confusing prefix sum indexing
- Ignoring initial prefix sum at -1
Solution
Step 1: Identify the bug
The code decrements freq for s[left] inside the while loop but does not check if freq reaches zero to decrement distinct_count.Step 2: Consequences of missing check
Without decrementing distinct_count, the while loop condition distinct_count > 2 never becomes false, causing infinite loop or incorrect window size.Final Answer:
Option A -> Option AQuick Check:
Missing distinct_count decrement inside while loop is a classic subtle bug [OK]
- Forgetting to decrement distinct_count
- Incorrect window size calculation
- Not breaking early in brute force
Solution
Step 1: Understand the variant
Duplicates count as separate elements, so sequences can include repeated numbers increasing length.Step 2: Evaluate approaches
Original hash set approach ignores duplicates; multiplying length by counts is incorrect because duplicates may be non-consecutive; sorting and scanning works but is O(n log n).Step 3: Correct approach
Using a hash map to store counts allows summing counts of consecutive numbers to find the longest run including duplicates efficiently.Final Answer:
Option A -> Option AQuick Check:
Hash map counting handles duplicates correctly and efficiently [OK]
- Ignoring duplicates
- Multiplying length by counts incorrectly
- Assuming sorting is always best
