Back to problems

Zigzag (Alternating-Parity) Subarrays

Algorithm · ByteDance · Medium

You are given an integer array nums. A subarray is called a zigzag subarray if, for every pair of adjacent elements in that subarray, one is even and the other is odd. Note that a subarray of length 1 automatically satisfies this alternating parity condition. Return the total number of contiguous zigzag subarrays within nums. Example 1: Explanation: The individual elements [2], [3], [4], [5] count, as do the length‑2 subarrays [2,3], [3,4], [4,5]. The length‑3 subarrays…

Loading the rest of the question…