Standard Bar Sliding Window · Medium Problems O(n) · O(1)
A freight yard tracks wagons, flagging each as carrying odd-classified cargo (1) or not (0 in the parity log). A route segment is 'eventful' when exactly k wagons in it carry odd-classified cargo. The yard clerk needs the number of eventful segments — even-length gaps of even cargo are allowed to pad either side, so segments are counted by position, not by shape.
Input: An integer array nums and an integer k.
Output: Return the number of contiguous subarrays containing exactly k odd numbers.
1 <= nums.length <= 500001 <= k <= nums.length0 <= nums[i] <= 10^5Input: {"nums":[1,1,2,1,1],"k":3}
Output: 2
Only [1,1,2,1] and [1,2,1,1] hold exactly three odd wagons.
Input: {"nums":[2,4,6],"k":1}
Output: 0
No wagon carries odd cargo, so no segment can be eventful.