Subarray With Elements Greater Than Varying Threshold - Problem

You are given an integer array nums and an integer threshold.

Find any subarray of nums of length k such that every element in the subarray is greater than threshold / k.

Return the size of any such subarray. If there is no such subarray, return -1.

A subarray is a contiguous non-empty sequence of elements within an array.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,3,3,3], threshold = 6
Output: 3
💡 Note: For k=3, threshold/k = 6/3 = 2. The subarray [3,3,3] has all elements > 2, so return 3.
Example 2 — Single Element
$ Input: nums = [6,5,6,5,8], threshold = 7
Output: 1
💡 Note: For k=1, threshold/k = 7/1 = 7. Element 8 > 7, so return 1.
Example 3 — No Valid Subarray
$ Input: nums = [1,1,1,1], threshold = 8
Output: -1
💡 Note: No subarray satisfies the condition since all elements are 1 and 1 ≤ 8/k for any k ≥ 1.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • 1 ≤ threshold ≤ 109

Visualization

Tap to expand
Subarray With Elements Greater Than Varying ThresholdInput: nums = [1,3,3,3], threshold = 61333Find subarray of length k where all elements > threshold/kk=3: threshold/k = 6/3 = 2Valid: [3,3,3] > 2Output: 3
Understanding the Visualization
1
Input
Array [1,3,3,3] with threshold = 6
2
Process
Find subarray where all elements > threshold/k
3
Output
Return size of valid subarray: 3
Key Takeaway
🎯 Key Insight: Use monotonic stack to find maximum valid ranges efficiently, then check threshold conditions
Asked in
Google 12 Facebook 8 Amazon 6
18.5K Views
Medium Frequency
~35 min Avg. Time
412 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen