BackmediumTwo PointersCred

Min Length Bimodal Subsequence Solution

Problem Statement

Given an array of integers scores, where each score is either a positive integer or a negative integer, determine the minimum length of a subarray that contains at least one positive integer and one negative integer. If no such subarray exists, return -1.

Example 1
Input
[3, -1, 2, -1, 3]
Output
5

Explanation: Step-by-step: Given the array [3, -1, 2, -1, 3], we initialize two pointers, left and right, to the start of the array. We then slide the right pointer to the right, expanding the window, until we find a subarray with both positive and negative numbers. The length of this subarray is 5, which is the minimum length we can achieve. Therefore, the output is 5.

Example 2
Input
[1, 2, 3, -1, -2, -3]
Output
4

Explanation: Step-by-step: Given the array [1, 2, 3, -1, -2, -3], we initialize two pointers, left and right, to the start of the array. We then slide the right pointer to the right, expanding the window, until we find a subarray with both positive and negative numbers. The length of this subarray is 4, which is the minimum length we can achieve. Therefore, the output is 4.

Constraints

  • 1 <= scores.length <= 10^5
  • -10^6 <= scores[i] <= 10^6
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

Min Length Bimodal Subsequence — Problem Statement & Solution Guide

Two PointersMediumMixed
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of integers scores, where each score is either a positive integer or a negative integer, determine the minimum length of a subarray that contains at least one positive integer and one negative integer. If no such subarray exists, return -1.

Examples

Example 1

Input

[3, -1, 2, -1, 3]

Output

5

Explanation: Step-by-step: Given the array [3, -1, 2, -1, 3], we initialize two pointers, left and right, to the start of the array. We then slide the right pointer to the right, expanding the window, until we find a subarray with both positive and negative numbers. The length of this subarray is 5, which is the minimum length we can achieve. Therefore, the output is 5.

Example 2

Input

[1, 2, 3, -1, -2, -3]

Output

4

Explanation: Step-by-step: Given the array [1, 2, 3, -1, -2, -3], we initialize two pointers, left and right, to the start of the array. We then slide the right pointer to the right, expanding the window, until we find a subarray with both positive and negative numbers. The length of this subarray is 4, which is the minimum length we can achieve. Therefore, the output is 4.

Constraints

  • 1 <= scores.length <= 10^5
  • -10^6 <= scores[i] <= 10^6

Optimal Approach & Strategy

The optimized approach uses a two-pointer technique to find the minimum length subarray in O(n) time complexity. It iterates through the array and keeps track of the minimum length subarray found so far.

Brute Force Approach

The brute-force approach involves checking all possible subarrays of the input array, which has a time complexity of O(n²). This approach is not efficient for large inputs.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function minLengthBimodalSubsequence(scores) {
  if (scores.length === 0 || scores.length === 1) return -1;
  let minLen = Infinity;
  let left = 0, right = 0;
  while (right < scores.length) {
    if (scores[right] > 0 && scores[right - 1] < 0) {
      minLen = Math.min(minLen, right - left);
    }
    right++;
  }
  let minLen2 = Infinity;
  for (let i = 0; i < scores.length; i++) {
    if (scores[i] > 0) {
      let j = i;
      while (j < scores.length && scores[j] > 0) {
        j++;
      }
      let k = i;
      while (k < scores.length && scores[k] < 0) {
        k++;
      }
      minLen2 = Math.min(minLen2, k - i);
    }
  }
  return Math.min(minLen, minLen2) === Infinity ? -1 : Math.min(minLen, minLen2);
}

Asked in Top Tech Interviews

Cred

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.