FastPrepMaximum Even-Sum Adjacent Pairs in a Circular Array

Maximum Even-Sum Adjacent Pairs in a Circular Array

Tesla logoTesla● MediumFULLTIMEOA
Learn

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[]) → int

Examples

Example 1

nums = [5,7,9,6,3]return = 2

Choose the wraparound pair (5, 3) and the adjacent pair (7, 9).

Example 2

nums = [1,1,1,1,1,1]return = 3

Pair consecutive elements without using the wraparound edge. All three sums are even.

Example 3

nums = [1,2,3,4]return = 0

Every 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.

More Tesla problems

See Tesla hiring insights
public int maxEvenSumPairs(int[] nums) {
  // write your code here
}
nums[5,7,9,6,3]
expected2
Checking account…