Practice
threeSumSmaller([-2, 0, 1, 3], 2)?Solution
Step 1: Sort input and initialize variables
Sorted array is [-2, 0, 1, 3], count=0, n=4.Step 2: Iterate and count triplets
For i=0 (-2), check if sum of first three elements >= 2? -2+0+1= -1 < 2, continue. Use two pointers left=1, right=3. - curr_sum = -2 + 0 + 3 = 1 < 2 -> count += 3-1=2, left=2 - curr_sum = -2 + 1 + 3 = 2 not < 2 -> right=2 Now left=2, right=2 ends loop. For i=1 (0), sum of nums[1]+nums[2]+nums[3] = 0+1+3=4 >= 2 -> break loop. Total count=2.Final Answer:
Option A -> Option AQuick Check:
Matches expected output from example [OK]
- Off-by-one in counting pairs
- Not breaking early when minimal sum >= target
- Misusing pointers leading to wrong counts
import bisect
def count_pairs_binary_search(nums, K):
count = 0
n = len(nums)
for i in range(n - 1):
target = nums[i] + K
j = bisect.bisect_right(nums, target, i + 1) - 1
if j > i:
count += j - i
return count
print(count_pairs_binary_search([1, 2, 5], 3))
Solution
Step 1: Trace iteration i=0
nums[0] = 1, target = 1 + 3 = 4. bisect_right(nums, 4, 1) returns index 2 (nums[2]=5 >4), so j = 2 - 1 = 1. Count += 1 - 0 = 1.Step 2: Trace iteration i=1
nums[1] = 2, target = 2 + 3 = 5. bisect_right(nums, 5, 2) returns 3 (end of list), so j = 3 - 1 = 2. Count += 2 - 1 = 1. Total count = 2.Final Answer:
Option A -> Option AQuick Check:
Pairs are (1,2) and (2,5) with differences 1 and 3 respectively [OK]
- Off-by-one error in bisect index adjustment
- Counting pairs where i >= j
- Miscomputing target as nums[i] - K
Solution
Step 1: Identify the loops and operations
The algorithm uses a single for loop iterating over n elements once, with constant time operations inside.Step 2: Confirm no nested loops or costly operations
No nested loops or shifting; elements are overwritten in-place, so no O(n²) behavior.Final Answer:
Option A -> Option AQuick Check:
Single pass with constant time per element -> O(n) [OK]
- Confusing with brute force O(n²)
- Assuming sorting is needed
- Ignoring in-place overwrite cost
Solution
Step 1: Compare left_max update condition
The original correct code uses '>=' to update left_max, but here it uses '>' which skips updating when equal.Step 2: Understand impact
Not updating left_max when height[left] equals left_max causes undercounting trapped water in some cases.Final Answer:
Option B -> Option BQuick Check:
Changing '>' to '>=' fixes subtle undercount bug [OK]
- Using '>' instead of '>=' for max updates
- Incorrect pointer increments
- Misplaced water addition lines
Solution
Step 1: Understand reuse and duplicates impact
Allowing i ≤ j means pairs with the same element twice are valid; binary search must include current index.Step 2: Modify binary search start index
Starting bisect_right from i (not i+1) includes pairs where i == j; counting j ≥ i pairs correctly.Step 3: Adjust counting logic
Count pairs as j - i + 1 if including i == j, or carefully count pairs with j ≥ i.Final Answer:
Option A -> Option AQuick Check:
Starting search at i and counting pairs with j ≥ i handles reuse correctly [OK]
- Using bisect_left instead of bisect_right incorrectly
- Ignoring pairs where i == j
- Assuming two pointers cannot handle reuse
