Count Sawtooth Subarrays
Updated
Problem statement
A sawtooth sequence alternates between even and odd values: every adjacent pair contains one even value and one odd value.
Given an integer array arr, count the number of contiguous subarrays that are sawtooth sequences. Every non-empty subarray is eligible, and a single-element subarray is always valid.
Function
countSawtoothSubarrays(arr: int[]) → longExamples
Example 1
arr = [1,3,5,7,9]return = 5Every pair of adjacent values is odd-odd, so no subarray of length at least 2 is valid. The five single-element subarrays are valid, so the result is 5.
Example 2
arr = [1,2,1,2,1]return = 15The entire array alternates parity, so all 5 * 6 / 2 = 15 contiguous subarrays are valid.
Example 3
arr = [1,2,3,7,6,5]return = 12The only parity break is the adjacent odd pair 3, 7. The alternating runs are [1, 2, 3] and [7, 6, 5]; each run contributes 3 * 4 / 2 = 6 valid subarrays, for a total of 12.
More Hudson River Trading Online Assessment questions
- Count Palindrome Addition ChoicesOA · Seen Nov 2026
- Count Multiples of Three with Two SevensOA · Seen Oct 2026
- Recursively Expanded Shell Command CountsOA · Seen Oct 2026
- Rotate Matrix Regions Around the DiagonalsOA · Seen Oct 2026
- Maximum Diamond SumOA · Seen Oct 2026
- Smallest Most-Lit Street PositionOA · Seen Oct 2026
- Rearrange String by Alternating EndsOA · Seen Sep 2026
- Most Items Within Budget from a Start IndexOA · Seen Sep 2026
- Count Fancy NumbersOA · Seen Sep 2026
- Count Pieces After Othello MovesOA · Seen Sep 2026
Similar questions at other companies
- Speed Up a Slow FunctionCitadel · OA · PHONE SCREENCitadel interview questions
- Maximum Income from Asset TradingJefferies · OA
- Best Time to Buy and Sell StockFlexTrade · OAFlexTrade interview questions
- Maximum Total of Nonadjacent RatingsMorgan Stanley · OAMorgan Stanley interview questions
- Count Sawtooth SubarraysTikTok · OATikTok interview questions
- Count Alternating-Parity SubarraysVisa · OAVisa interview questions
- Find Largest Sum Contiguous SubarrayCisco · OACisco interview questions
- Maximum Sum of Non-Adjacent ElementsLSEG · OALSEG interview questions
- Maximum Score with Prime JumpsMicrosoft · OAMicrosoft interview questions
- Maximum Beauty by DeletionsMeesho · OA