Count Uniform-or-Alternating Parity Subarrays
Updated
Problem statement
You are given an integer array nums.
A contiguous subarray is valid when its parity pattern is one of:
- uniform: every value has the same parity, or
- alternating: every adjacent pair has different parity.
Every one-element and two-element subarray is valid. Return the total number of valid contiguous subarrays.
Function
countUniformOrAlternatingParitySubarrays(nums: int[]) → longExamples
Example 1
nums = [1,2,3,4]return = 10The entire array alternates, so every one of its 4 * 5 / 2 = 10 subarrays is valid.
Example 2
nums = [1,3,5,7]return = 10All values are odd, so every subarray has uniform parity.
Example 3
nums = [1,2,4]return = 5All three singletons and both length-two subarrays are valid. The full parity pattern odd-even-even is neither uniform nor alternating.
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
- Maximum Score with Prime JumpsMicrosoft · OAMicrosoft interview questions
- Maximum Beauty by DeletionsMeesho · OA
- Card Game: Best First-Player ScoreShopify · OAShopify interview questions
- Maximum Game Score with Fixed Forward JumpsStripe · OAStripe interview questions
- 0/1 KnapsackConfluent · OAConfluent interview questions
- Minimum Cost to Tile a Strip with Widths 1, 2, and 3Infosys · OAInfosys interview questions
- Maximum Subarray SumStraive · OA
- Longest Increasing Price SubsequenceOpenAI · OAOpenAI interview questions
- Lowest-Cost Shirt Color ScheduleElevenLabs · OAElevenLabs interview questions
- Maximum Game Board ScoreJP Morgan Chase · OAJP Morgan Chase interview questions