BudiBadu Logo
00:00

Longest Balanced Parity Subarray

Array Medium 0 views

You are analyzing a stream of integers generated by a monitoring system. Each even number represents a phase of predictable behavior, while each odd number reflects periods of experimentation. You want to identify a contiguous stretch where exploration and predictability coexist evenly, meaning the counts of even and odd values are exactly the same. Instead of simply reporting the length of that stretch, the platform needs the actual sequence, formatted as a comma-separated string, so analysts can instantly visualize the pattern that emerged.

Imagine a long tape filled with colored markers. Blue markers represent even values, red markers stand for odd values. Your job is to slide a transparent window over the tape and capture the longest segment where the number of blue and red markers match perfectly. When that segment is found, you preserve its order and hand it off as a string joined by commas. If multiple segments share the same maximum length, you can return the first one you encounter. The sequence must remain contiguous to reflect the original timeline.

Edge cases require care. When the array is empty or no balanced segment exists, the correct response is an empty string. Negative numbers follow the same parity rules, so an odd negative value counts the same as a positive odd value. This task rewards your ability to detect equilibrium within fluctuating data and to present the findings in a human-friendly format.

Example 1:

Input: nums = [2,5,6,3,4,7]
Output: "2,5,6,3,4,7"
Explanation: The entire array has three evens and three odds.

Example 2:

Input: nums = [1,3,5,7]
Output: ""
Explanation: No contiguous subarray balances even and odd counts.

Example 3:

Input: nums = [2,4,1,3,6,8,5,7]
Output: "2,4,1,3,6,8,5,7"
Explanation: The full range contains four even and four odd numbers.

Related Problems

No related problems found

Comments (0)

Join the Discussion

Share your thoughts, ask questions, or help others with this problem.

BudiBadu Logo

Longest Balanced Parity Subarray

Array Medium 0 views

You are analyzing a stream of integers generated by a monitoring system. Each even number represents a phase of predictable behavior, while each odd number reflects periods of experimentation. You want to identify a contiguous stretch where exploration and predictability coexist evenly, meaning the counts of even and odd values are exactly the same. Instead of simply reporting the length of that stretch, the platform needs the actual sequence, formatted as a comma-separated string, so analysts can instantly visualize the pattern that emerged.

Imagine a long tape filled with colored markers. Blue markers represent even values, red markers stand for odd values. Your job is to slide a transparent window over the tape and capture the longest segment where the number of blue and red markers match perfectly. When that segment is found, you preserve its order and hand it off as a string joined by commas. If multiple segments share the same maximum length, you can return the first one you encounter. The sequence must remain contiguous to reflect the original timeline.

Edge cases require care. When the array is empty or no balanced segment exists, the correct response is an empty string. Negative numbers follow the same parity rules, so an odd negative value counts the same as a positive odd value. This task rewards your ability to detect equilibrium within fluctuating data and to present the findings in a human-friendly format.

Example 1:

Input: nums = [2,5,6,3,4,7]
Output: "2,5,6,3,4,7"
Explanation: The entire array has three evens and three odds.

Example 2:

Input: nums = [1,3,5,7]
Output: ""
Explanation: No contiguous subarray balances even and odd counts.

Example 3:

Input: nums = [2,4,1,3,6,8,5,7]
Output: "2,4,1,3,6,8,5,7"
Explanation: The full range contains four even and four odd numbers.

00:00
Loading editor...
Test Results

Run your code to see test results

Click the Submit button to execute your solution

Related Problems

No related problems found

Comments (0)

Join the Discussion

Share your thoughts, ask questions, or help others with this problem.