Company: Visa_2_Jan
Difficulty: medium
Count Sawtooth Subarrays Problem Description A sawtooth sequence is a sequence of numbers that alternate between even and odd. In other words, each element is of different parity than both of its neighboring elements. Given an array of integers arr , your task is to count the number of contiguous subarrays that represent a sawtooth sequence. Note that any subarray of length 1 is considered a sawtooth sequence. Examples Example 1: Input: arr = [1, 3, 5, 7, 9] Output: 5 Explanation: Since all the elements are odd, it's not possible to form any sawtooth subarrays of length 2 or more. There are 5 possible subarrays containing one element: [1] , [3] , [5] , [7] , [9] . So the answer is 5. Example 2: Input: arr = [1, 2, 1, 2, 1] Output: 15 Explanation: All contiguous subarrays satisfy the condition of alternating parity. An array of length 5 has (5 * (5 + 1)) / 2 = 15 possible contiguous subarrays. So the answer is 15. Example 3: Input: arr = [1, 2, 3, 7, 6, 5] Output: 12 Explanation: The ar