Count adjacent pairs of numbers that sum to a target value.
Write a function `count_adjacent_pairs(nums, target)` that takes a list of integers `nums` and an integer `target`. The function should count how many pairs of adjacent elements in `nums` sum to `target`. A pair is adjacent if the two elements are at indices `i` and `i+1` for some `i`. Each distinct adjacent pair is counted once. For example, in `[1, 2, 2, 3]` with target 4, the pairs `(1,2)` and `(2,2)` and `(2,3)` are adjacent pairs; the pairs `(1,2)` and `(2,3)` sum to 4, but `(2,2)` sums to 4 as well, so the count is 3. Wait, re-evaluate: `(1,2)` sum 3, `(2,2)` sum 4, `(2,3)` sum 5. So only `(2,2)` sums to 4, count is 1. But the example in the problem says 2? Let's clarify: the example `[1,2,2,3]` target 4: pairs are (1,2)=3, (2,2)=4, (2,3)=5, so count = 1. The QA error says expected 2, which is incorrect for adjacent pairs. The correct expected is 1. Therefore the problem is to count adjacent pairs, but the test case was wrong. We must fix the test case. The solution code counts adjacent pairs correctly and fails because test expected wrong. We need to correct the test case to match the adjacent definition. The correct expected for `[1,2,2,3],4` is 1. The all_ones case `[1,1,1,1],2` has adjacent pairs (1,1) at indices (0,1), (1,2), (2,3) all sum to 2, so count 3, that's correct. The typical case `[1,2,3,4],5`: (1,2)=3, (2,3)=5, (3,4)=7 so count 1, correct. So we fix the adjacent_duplicates test case to expected 1. Also we might rename function to avoid confusion with 'distinct' which could mean value distinct, but we keep clear. The problem is about adjacent pairs, not distinct values. So the title could be 'Count Adjacent Pairs' and function name 'count_adjacent_pairs' to match. We need to ensure starter code uses same function name as tests. The original had `count_distinct_pairs` but we can change to `count_adjacent_pairs`. The problem statement mentions distinct pairs but actually it's just adjacent pairs. We'll rename to avoid ambiguity. Also update example code to match the corrected test. The solution code is correct but we need to adjust the test case. Also maybe add a case where there are duplicate adjacent pairs like [1,2,1,2] target 3 gives 2, but that's fine. We'll keep all existing test cases but fix the expected for adjacent_duplicates to 1. Also ensure function name consistent. The statement and example must match. We'll produce a corrected JSON.
Constraints
The input list length is between 0 and 10^5. Each element is an integer. Target is an integer. The solution should run in O(n) time and O(1) extra space.
Example
>>> count_adjacent_pairs([1, 2, 3, 4], 5)
1
>>> count_adjacent_pairs([1, 2, 2, 3], 4)
1
>>> count_adjacent_pairs([], 0)
0
10 points
~15 min