Back to problems

Sawtooth (Alternating Parity) Subarray Count

Algorithm · Capital One · Medium

Requirements You receive an integer array arr containing at most approximately 10^5 elements. Call a contiguous subarray sawtooth when each neighboring pair consists of one odd value and one even value. A subarray containing just one item is valid by definition. Compute how many contiguous sawtooth subarrays exist in total. Examples In the third case, the valid subarrays are [5], [5,8], [5,8,1], [8], [8,1], [1], [7], [7,4], [7,4,9], [4], [4,9], [9]. Preparation

Checking your access…