Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

795. Number of Subarrays with Bounded Maximum

Medium53.4% Acceptance
ArrayTwo Pointers
Asked by:
Q
Quora
Amazon
ProblemSolutions (8)VideosCompanies (3)Notes

Problem Statement

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 <= 105
  • 0 <= nums[i] <= 109
  • 0 <= left <= right <= 109
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
A
Adobe

Approach

To solve #795 Number of Subarrays with Bounded Maximum, the goal is to count subarrays where the maximum element lies within a given range [left, right]. A brute-force approach would check every subarray and compute its maximum, but this leads to O(n^2) or worse, which is inefficient for large inputs.

A more optimal strategy uses the two pointers / sliding window technique. Instead of explicitly calculating the maximum for every subarray, track the positions where elements exceed the upper bound and where valid elements occur. By maintaining counters for the most recent invalid element and the most recent element within the valid range, you can determine how many valid subarrays end at the current index.

This transforms the problem into counting valid segments dynamically while scanning the array once. The optimized approach achieves linear time complexity and uses constant extra space, making it efficient for interview scenarios.

Complexity

ApproachTime ComplexitySpace Complexity
Sliding Window / Two PointersO(n)O(1)

Video Solution Available

NeetCode

View all video solutions

Solutions (8)

Consider Maximum Allowed Value

To solve this problem, we iterate through the array and use two helper functions to find the number of subarrays with maximum elements less than or equal to `right` and strictly less than `left`. The result is the difference between these two values.

Time Complexity: O(n), where n is the length of the array because we pass through the array twice both in O(n) time.
Space Complexity: O(1) as we only use a fixed amount of extra space.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3int countSubarrays(int* nums, int numsSize, int bound) {
4    int count = 0, result = 

Explanation

The function countSubarrays counts the number of subarrays where the maximum element is less than or equal to a given bound. By computing this for both the 'right' and 'left-1' bounds and subtracting the two results, we can find the count where the maximum element lies between 'left' and 'right'.

Sliding Window Technique

This approach utilizes a sliding window to dynamically check and adjust the range within the array where subarrays satisfy the maximum constraints between `left` and `right`. We scan and adjust pointers to pinpoint valid ranges continuously.

Time Complexity: O(n), scanning and resolving limits in a single pass.
Space Complexity: O(1) maintaining concise storage need.

PythonJavaScript
1def numSubarrayBoundedMax(nums, left, right):
2

Video Solutions

Watch expert explanations and walkthroughs

Maximum Subarray - Amazon Coding Interview Question - Leetcode 53 - Python

NeetCode
8:03605,117 views

Asked By Companies

3 companies
Q
Quora
A
Amazon
A
Adobe

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Container With Most WaterMedium
3SumMedium
3Sum ClosestMedium
4SumMedium
More similar problems

Related Topics

ArrayTwo Pointers

Problem Stats

Acceptance Rate53.4%
DifficultyMedium
Companies3

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why is the sliding window technique useful for this problem?

The sliding window technique helps process subarrays dynamically without recomputing maximum values for each range. By maintaining boundary conditions during traversal, it efficiently counts valid subarrays in linear time.

Is Number of Subarrays with Bounded Maximum asked in FAANG interviews?

Yes, problems involving subarray counting and sliding window techniques are common in FAANG-style interviews. This question tests a candidate's ability to transform brute-force subarray enumeration into an efficient linear-time solution.

What data structure is best for Number of Subarrays with Bounded Maximum?

No complex data structure is required for this problem. Simple variables combined with array traversal are enough to track boundaries and counts efficiently while applying the sliding window idea.

What is the optimal approach for Number of Subarrays with Bounded Maximum?

The optimal approach uses a sliding window or two-pointer technique. By tracking the last index where the value exceeded the upper bound and the last valid element within range, you can count valid subarrays in a single pass. This reduces the time complexity to O(n).

0
,
current
=
0
;
5
for
(
int
i
=
0
;
i
<
numsSize
;
i
++
)
{
6
if
(
nums
[
i
]
<=
bound
)
{
7
current
++
;
8
}
else
{
9
current
=
0
;
10
}
11
result
+=
current
;
12
}
13
return
result
;
14
}
15
16
int
numSubarrayBoundedMax
(
int
*
nums
,
int
numsSize
,
int
left
,
int
right
)
{
17
return
countSubarrays
(
nums
,
numsSize
,
right
)
-
countSubarrays
(
nums
,
numsSize
,
left
-
1
)
;
18
}
19
20
int
main
(
)
{
21
int
nums
[
]
=
{
2
,
1
,
4
,
3
}
;
22
int
left
=
2
,
right
=
3
;
23
printf
(
"%d\n"
,
numSubarrayBoundedMax
(
nums
,
4
,
left
,
right
)
)
;
24
return
0
;
25
}
def
count
(
bound
)
:
3
ans
=
0
4
subarray_count
=
0
5
for
x
in
nums
:
6
if
x
<=
bound
:
7
subarray_count
+=
1
8
else
:
9
subarray_count
=
0
10
ans
+=
subarray_count
11
return
ans
12
13
return
count
(
right
)
-
count
(
left
-
1
)
14
15
nums
=
[
2
,
1
,
4
,
3
]
16
left
=
2
17
right
=
3
18
print
(
numSubarrayBoundedMax
(
nums
,
left
,
right
)
)

Explanation

The sliding window approach in Python for the specified problem uses a queue-like logic to maintain ranges that fall within the boundaries. Counter increments track the number of valid sequences built during iterations.