Practice
k. You need to count the number of contiguous subarrays that contain exactly k odd numbers. Which approach guarantees an optimal solution with linear time complexity?Solution
Step 1: Understand the problem constraints
The problem requires counting subarrays with exactly k odd numbers efficiently.Step 2: Evaluate approaches
Brute force is O(n^2), DP is complex and not straightforward here, greedy alone doesn't guarantee correctness. Sliding window with two pointers counting at most k and k-1 odd numbers and subtracting is optimal and linear.Final Answer:
Option C -> Option CQuick Check:
Sliding window approach runs in O(n) time [OK]
- Thinking greedy alone solves it optimally
- Assuming DP is needed
- Using brute force for large inputs
nums = [3,1,1,1,5], x = 5, what is the returned value?Solution
Step 1: Calculate totalSum and target
totalSum = 3+1+1+1+5 = 11; target = 11 - 5 = 6.Step 2: Use sliding window to find longest subarray summing to 6
Window expands: [3] sum=3, [3,1] sum=4, [3,1,1] sum=5, [3,1,1,1] sum=6 -> max_len=4; next, add 5 sum=11 > 6, shrink from left: remove 3 sum=8 >6, remove 1 sum=7 >6, remove 1 sum=6 -> max_len=4 remains (subarray [1,1,1]).Step 3: Calculate minimum operations
Minimum removals = array length - max_len = 5 - 4 = 1.Final Answer:
Option D -> Option DQuick Check:
Longest subarray length 4 -> min ops = 1 [OK]
- Off-by-one errors in window length calculation
- Miscomputing sums when shrinking window
- Confusing min operations with longest subarray length
from typing import List
def summaryRanges(nums: List[int]) -> List[str]:
ranges = []
n = len(nums)
start = 0
while start < n:
end = start
while end + 1 < n and nums[end + 1] == nums[end] + 1:
end += 1
if start == end:
ranges.append(str(nums[start]))
else:
ranges.append(f"{nums[start]}->{nums[end]}")
start = end + 1
return ranges
Solution
Step 1: Trace first range detection
start=0 (nums[0]=1), end moves from 0 to 2 because nums[1]=2 and nums[2]=3 are consecutive.Step 2: Append first range and move start
Append '1->3', then start=3 (nums[3]=5), end=3, no consecutive next element, append '5'.Final Answer:
Option B -> Option BQuick Check:
Ranges correctly summarize consecutive sequences [OK]
- Appending single elements as ranges like '5->5'
- Splitting consecutive sequences incorrectly
- Off-by-one errors in inner loop
Solution
Step 1: Identify the single loop over the array
The algorithm iterates once over n elements, updating prefix_sum and hash map.Step 2: Confirm hash map operations are O(1) average
Insertion and lookup in hash map are average O(1), so total time is O(n).Final Answer:
Option A -> Option AQuick Check:
Brute force is O(n²), no sorting involved, space is O(n) not O(n²) [OK]
- Confusing brute force O(n²) with optimal
- Assuming hash map is O(log n)
- Overestimating space complexity
p in s?Solution
Step 1: Analyze the main loop over string
The algorithm slides a window of sizesm = len(p)oversonce, performing O(1) updates per step.Step 2: Consider frequency array operations
Frequency arrays are fixed size 26, so comparisons and updates are O(1) per iteration, not dependent onm.Final Answer:
Option C -> Option CQuick Check:
Sliding window with fixed alphabet size -> O(n) time [OK]
- Confusing frequency array size with input size
- Assuming nested loops cause O(n*m)
- Ignoring constant alphabet size
