Number of Subarrays with Bounded Maximum
MEDIUMDescription
Given an integer array nums and two integers left and right, return the number of contiguous non-empty subarrays such that the value of the maximum array element in that subarray is in the range [left, right].
The test cases are generated so that the answer will fit in a 32-bit integer.
Example 1:
Input: nums = [2,1,4,3], left = 2, right = 3 Output: 3 Explanation: There are three subarrays that meet the requirements: [2], [2, 1], [3].
Example 2:
Input: nums = [2,9,2,5,6], left = 2, right = 8 Output: 7
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 1090 <= left <= right <= 109
Approaches
Checkout 4 different approaches to solve Number of Subarrays with Bounded Maximum. Click on different approaches to view the approach and algorithm in detail.
Brute Force Approach
The most straightforward approach is to generate every possible contiguous subarray, find the maximum element for each one, and then check if this maximum falls within the given bounds [left, right]. This method is easy to conceptualize but computationally expensive.
Algorithm
- Initialize a counter
countto 0. - Use a nested loop with index
ifrom0ton-1to select the starting element of the subarray. - Use another nested loop with index
jfromiton-1to select the ending element of the subarray. - For each subarray
nums[i...j], use a third loop with indexkfromitojto find the maximum elementmaxVal. - Check if
maxValis within the range[left, right]. If it is, incrementcount. - After all loops complete, return
count.
This approach systematically checks every single subarray. It uses a pair of nested loops to define the start and end points of a subarray. For each subarray defined, a third loop is used to iterate through its elements to find the maximum value. This maximum is then compared against the left and right bounds. If it satisfies the condition left <= max <= right, a counter is incremented. While correct, the cubic time complexity makes it impractical for the given constraints.
class Solution {
public int numSubarrayBoundedMax(int[] nums, int left, int right) {
int count = 0;
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int maxVal = -1;
// Find max in the subarray nums[i...j]
for (int k = i; k <= j; k++) {
maxVal = Math.max(maxVal, nums[k]);
}
if (maxVal >= left && maxVal <= right) {
count++;
}
}
}
return count;
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement.
- Guaranteed to be correct.
- Extremely inefficient due to three nested loops.
- Will result in a 'Time Limit Exceeded' error for even moderately sized inputs.
Code Solutions
Checking out 3 solutions in different languages for Number of Subarrays with Bounded Maximum. Click on different languages to view the code.
class Solution {
public
int numSubarrayBoundedMax(int[] nums, int left, int right) {
return f(nums, right) - f(nums, left - 1);
}
private
int f(int[] nums, int x) {
int cnt = 0, t = 0;
for (int v : nums) {
t = v > x ? 0 : t + 1;
cnt += t;
}
return cnt;
}
}
Video Solution
Watch the video walkthrough for Number of Subarrays with Bounded Maximum
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.