Count Alternating-Parity Subarrays
Updated
Problem statement
You are given an integer array nums.
A contiguous subarray is alternating when every pair of adjacent elements has different parity: one is even and the other is odd. Every one-element subarray is alternating.
Return the total number of alternating contiguous subarrays.
Function
countAlternatingParitySubarrays(nums: int[]) → longExamples
Example 1
nums = [1,3,5,7,9]return = 5Every adjacent pair has odd-odd parity, so only the five one-element subarrays qualify.
Example 2
nums = [1,2,1,2,1]return = 15The entire array alternates. All 5 * 6 / 2 = 15 contiguous subarrays therefore qualify.
Example 3
nums = [1,2,3,7,6,5]return = 12The equal-parity pair 3, 7 splits the array into two alternating runs of length 3. Each contributes 3 * 4 / 2 = 6 subarrays.
Constraints
1 <= nums.length <= 105-109 <= nums[i] <= 109- The answer fits in a signed 64-bit integer.
More Visa Online Assessment questions
- Count Prime Values in SubtreesOA · Seen Nov 2026
- Lock CodeOA · Seen Nov 2026
- Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
- Longest Non-Decreasing Substring After Pair SwapsOA · Seen Oct 2026
- Count Attack Vectors by Divisor RangeOA · Seen Oct 2026
- Minimum Operations to Reduce a Transaction LedgerOA · Seen Oct 2026
- Count Valid Space-Delimited WordsOA · Seen Oct 2026
- Earliest Common Meeting StartOA · Seen Oct 2026
- Maximum Index-Weighted Sum with Disjoint SwapsOA · Seen Oct 2026
- Maximum Equalized Substrings After One Adjacent SwapOA · 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
- Count Sawtooth SubarraysHudson River Trading · OAHudson River Trading interview questions
- Maximum Total of Nonadjacent RatingsMorgan Stanley · OAMorgan Stanley interview questions
- Count Sawtooth SubarraysTikTok · OATikTok 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