Ctrl K

Chain With Same-Parity Sums

Medium
You get an array of positive integers readings. Pick a chain from it (some elements in their original order, not necessarily adjacent) such that the sums of all neighbouring pairs in the chain have the same parity: either all even or all odd. Return the length of the longest such chain. Any two elements always form a valid chain.
Example 1
readings=[5, 2, 8, 3, 6] → 4

The chain 5, 2, 3, 6 works: every neighbouring sum (7, 5, 9) is odd. Nothing longer fits.

Example 2
readings=[7, 9, 11, 4] → 3

Take the three odd numbers: every neighbouring sum is even.

Example 3
readings=[10, 3] → 2
Constraints
  • 2 ≤ len(readings) ≤ 2·10⁵
  • 1 ≤ readings[i] ≤ 10⁷
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route Medium
loading Python…
Input
readings=[5, 2, 8, 3, 6]
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 110 tests on the server, hidden ones included.