Dsa
Prefix Sum
Easy
The Parity Marathon
You are given a sequence of N integers. A 'Balanced Segment' is a contiguous subsequence that contains an equal number of even and odd numbers. Find the length of the longest Balanced Segment in the sequence.
Example:
Input:
5
1 2 3 4 6
Output:
4
Explanation:
The segment [1, 2, 3, 4] contains two odds (1, 3) and two evens (2, 4). Length is 4. The whole array [1, 2, 3, 4, 6] has 2 odds and 3 evens.
Key concepts
prefix_sumhashmapparity
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now