Ctrl K

Even-Led Zigzag Under a Cap

Easy
You are given an array of positive integers nums and an integer cap. Find the longest contiguous stretch of the array such that: - its first element is even; - neighbouring elements alternate in parity: even, odd, even, …; - no element is greater than cap. Return the length of that stretch, or 0 if no stretch qualifies.
Example 1
nums=[7, 4, 9, 2, 5, 8], cap=8 → 3

[4, 9, 2, 5, 8] starts even and alternates parity, but 9 exceeds 8. The best choice is [2, 5, 8], length 3.

Example 2
nums=[3, 5, 7], cap=10 → 0

There are no even numbers, so no stretch can start: the answer is 0.

Example 3
nums=[6, 6, 1, 4], cap=6 → 3

The two 6s share a parity, so the best stretch is [6, 1, 4].

Constraints
  • 1 ≤ len(nums) ≤ 100
  • 1 ≤ nums[i] ≤ 100
  • 1 ≤ cap ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRepeat Within Reach EasyValues One Apart EasyBest Window Average EasyStep Streak Score EasyNeighbour Sums Around a Circle Easy
loading Python…
Input
nums=[7, 4, 9, 2, 5, 8], cap=8
Expected
3

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