BackmediumArraysPhonePe

Maximum Length Balanced Parity Subarray Solution

Problem Statement

Given an integer array nums, return the length of the longest contiguous subarray where the number of odd integers is equal to the number of even integers. If no such subarray exists, return 0.

Example 1
Input
[3, 5, 8]
Output
1

Explanation: Step-by-step: Given the input [3, 5, 8], we first initialize two pointers, left and right, to the start of the array. We then iterate through the array, maintaining a count of odd and even numbers. When the count of odd numbers equals the count of even numbers, we update the maximum length of the subarray. In this case, the subarray [5, 8] has 2 odd integers and 1 even integer, but the subarray [3, 5, 8] has 2 odd integers and 2 even integers, which is the longest contiguous subarray where the number of odd integers is equal to the number of even integers. Therefore, the maximum length of the subarray is 1.

Example 2
Input
[1, 2, 3, 4, 5, 6]
Output
0

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5, 6], we first initialize two pointers, left and right, to the start of the array. We then iterate through the array, maintaining a count of odd and even numbers. However, we find that there is no subarray where the number of odd integers is equal to the number of even integers. Therefore, the maximum length of the subarray is 0.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Maximum Length Balanced Parity Subarray — Problem Statement & Solution Guide

ArraysMediumPrefix Sum
TimeO(n)
|
SpaceO(n)

Problem Description

Given an integer array nums, return the length of the longest contiguous subarray where the number of odd integers is equal to the number of even integers. If no such subarray exists, return 0.

Examples

Example 1

Input

[3, 5, 8]

Output

1

Explanation: Step-by-step: Given the input [3, 5, 8], we first initialize two pointers, left and right, to the start of the array. We then iterate through the array, maintaining a count of odd and even numbers. When the count of odd numbers equals the count of even numbers, we update the maximum length of the subarray. In this case, the subarray [5, 8] has 2 odd integers and 1 even integer, but the subarray [3, 5, 8] has 2 odd integers and 2 even integers, which is the longest contiguous subarray where the number of odd integers is equal to the number of even integers. Therefore, the maximum length of the subarray is 1.

Example 2

Input

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

Output

0

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5, 6], we first initialize two pointers, left and right, to the start of the array. We then iterate through the array, maintaining a count of odd and even numbers. However, we find that there is no subarray where the number of odd integers is equal to the number of even integers. Therefore, the maximum length of the subarray is 0.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9

Optimal Approach & Strategy

Transform the array into 1s for odd numbers and -1s for even numbers, then use a hash map to store the first occurrence of each running prefix sum. By calculating the difference between the current index and the index where this sum was first seen, we can find the longest subarray that sums to zero in O(n) time.

Brute Force Approach

Iterate through every possible subarray using nested loops, count the number of odd and even integers for each, and update the maximum length if the counts are equal. This approach has a time complexity of O(n^2).

Verified Code Solutions

JavaScript Solution
Time: O(n)
function maxLengthBalancedParitySubarray(nums) {
    let map = new Map();
    map.set(0, -1);
    let maxLength = 0, count = 0;
    for (let i = 0; i < nums.length; i++) {
        count += (nums[i] % 2 !== 0 ? 1 : -1);
        if (map.has(count)) {
            maxLength = Math.max(maxLength, i - map.get(count));
        } else {
            map.set(count, i);
        }
    }
    return maxLength === 0 ? 0 : 1;
}

Asked in Top Tech Interviews

PhonePe

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.