FunctionRacerProblemsRace

Even-Odd Balanced Cuts

EasyGreedyPython, JavaScript, TypeScript, Java, C++

nums contains exactly as many even numbers as odd numbers (0 is even, and negative numbers count by their parity as usual). Cut nums into as many pieces as possible so that every piece is a contiguous block that also contains equally many even and odd numbers. Return the largest possible number of pieces. The whole uncut list is always one valid piece; return 0 if nums is empty.

Example 1

nums [2,1,4,3,6,5]
returns 3

[2, 1], [4, 3] and [6, 5] each hold one even and one odd number; no piece can be shorter than two items.

Example 2

nums [1,3,2,4]
returns 1

The first two items are both odd, so no cut can come after one or two items; the only valid piece is the whole list.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Greedy problems