BudiBadu Logo

Balanced Triplets Subarray

Sliding Window Easy 3 views
Like11

You are studying a timeline of sensor readings represented as integers. Engineers noticed that certain segments exhibit a rhythmic pattern: looking at every sliding window of three consecutive readings, exactly one value is even while the remaining two are odd. These triplets indicate that the system is alternating between stability checks and exploratory bursts in a predictable cadence. Your task is to find any longest contiguous subarray that preserves this property across its entire span. Instead of providing the length, you must return the actual sequence that satisfies the criteria, keeping the order intact.

Imagine a ribbon divided into numbered tiles. As you slide a frame of width three along the ribbon, you want every frame to reveal exactly one blue tile (even) and two red tiles (odd). The moment a frame breaks this balance—perhaps two blues appear together or all three are red—the pattern is interrupted and the subarray must end. Among all subarrays that maintain the rule throughout, identify one of the longest and hand it back as a collection so other teams can run deeper analysis on the exact readings.

If no segment satisfies the condition for at least three elements, return an empty collection. Arrays shorter than three can only qualify if they naturally meet the rule for the windows they contain. Negative values follow the same parity logic based on their remainder modulo two. This challenge emphasizes careful pattern detection combined with the ability to return structured data that reflects the sensor timeline precisely.

Example 1:

Input: nums = [1,2,3,5,4,7,9]
Output: [1,2,3,5,4,7]
Explanation: Sliding windows like [1,2,3], [2,3,5], [3,5,4], [5,4,7] each contain exactly one even and two odd numbers.

Example 2:

Input: nums = [2,4,6,8]
Output: []
Explanation: No window of three values has the required mix.

Example 3:

Input: nums = [1,3,2,5,7,4,9,11,6]
Output: [1,3,2,5,7,4,9]
Explanation: The first seven readings sustain the one-even two-odd pattern across every triple.

Algorithm Flow

Recommendation Algorithm Flow for Balanced Triplets Subarray - Budibadu
Recommendation Algorithm Flow for Balanced Triplets Subarray - Budibadu

Best Answers

java
import java.util.*;
class Solution {
    public int[] balanced_triplets_subarray(int[] nums) {
        int n = nums.length;
        if (n < 3) return new int[0];
        
        boolean[] valid_starts = new boolean[n - 2];
        for (int i = 0; i < n - 2; i++) {
            int odd_cnt = 0;
            for (int j = 0; j < 3; j++) {
                if (nums[i+j] % 2 != 0) odd_cnt++;
            }
            if (odd_cnt == 2) valid_starts[i] = true;
        }
        
        int longest_len = 0;
        int best_start = -1;
        int current_start = -1;
        int current_len = 0;
        
        for (int i = 0; i < n - 2; i++) {
            if (valid_starts[i]) {
                if (current_len == 0) current_start = i;
                current_len++;
            } else {
                if (current_len > 0) {
                    int real_len = current_len + 2;
                    if (real_len > longest_len) {
                        longest_len = real_len;
                        best_start = current_start;
                    }
                }
                current_len = 0;
            }
        }
        
        if (current_len > 0) {
            int real_len = current_len + 2;
            if (real_len > longest_len) {
                longest_len = real_len;
                best_start = current_start;
            }
        }
        
        if (best_start == -1) return new int[0];
        return Arrays.copyOfRange(nums, best_start, best_start + longest_len);
    }
}