Min Length Bimodal Subsequence ā Problem Statement & Solution Guide
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
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.
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
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);
}class Solution {
public int min_length_bimodal_subsequence(int[] nums) {
int left = 0;
int right = 0;
int min_length = Integer.MAX_VALUE;
while (right < nums.length) {
if (nums[right] * nums[left] > 0) {
min_length = Math.min(min_length, right - left + 1);
left += 1;
} else {
right += 1;
}
}
return min_length == Integer.MAX_VALUE ? -1 : min_length;
}
}def min_length_bimodal_subsequence(nums):
left = 0
right = 0
min_length = float('inf')
while right < len(nums):
if nums[right] * nums[left] > 0:
min_length = min(min_length, right - left + 1)
left += 1
else:
right += 1
return min_length if min_length != float('inf') else -1function 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
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.