Maximum Even-Sum Adjacent Pairs in a Circular Array
Problem statement
You are given a circular array of integers nums. Indices i and (i + 1) mod n are adjacent.
Choose as many disjoint adjacent pairs as possible such that the sum of the two values in every chosen pair is even. Each array element may belong to at most one chosen pair.
Return the maximum possible number of pairs.
Function
maxEvenSumPairs(nums: int[]) → intExamples
Example 1
nums = [5,7,9,6,3]return = 2Choose the wraparound pair (5, 3) and the adjacent pair (7, 9).
Example 2
nums = [1,1,1,1,1,1]return = 3Pair consecutive elements without using the wraparound edge. All three sums are even.
Example 3
nums = [1,2,3,4]return = 0Every adjacent pair contains one odd and one even value, so every adjacent sum is odd.
Constraints
1 <= nums.length <= 2 * 10^5.-10^9 <= nums[i] <= 10^9.