Subarray With Elements Greater Than Varying Threshold
01 · Problem
You are given an integer array nums and an integer threshold.
A non-empty contiguous subarray of length L is valid when every element in it is strictly greater than threshold / L (real division, not integer division).
Return the smallest length L of any valid subarray. If no valid subarray exists, return -1.
02 · Examples
nums = [1,3,4,3,1], threshold = 6
3
For length 3 every element must exceed 6 / 3 = 2, and [3,4,3] works. Length 1 needs an element above 6 and length 2 needs two adjacent elements above 3, neither of which exists.
nums = [6,5,6,5,8], threshold = 7
1
The single element 8 is greater than 7 / 1 = 7, so length 1 already works and it is the smallest possible.
nums = [1,1,1], threshold = 10
-1
Even the whole array (length 3) would need every element above 10 / 3, so no valid subarray exists.
03 · Constraints
- 011 <= nums.length <= 105
- 021 <= nums[i] <= 109
- 031 <= threshold <= 109
04 · Optimal complexity
- Time
- O(n)
- Space
- O(n)
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.