Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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.
Example 1:
Input: nums = [1,3,4,3,1], threshold = 6 Output: 3 Explanation: The subarray [3,4,3] has a size of 3, and every element is greater than 6 / 3 = 2. Note that this is the only valid subarray.
Example 2:
Input: nums = [6,5,6,5,8], threshold = 7 Output: 1 Explanation: The subarray [8] has a size of 1, and 8 > 7 / 1 = 7. So 1 is returned. Note that the subarray [6,5] has a size of 2, and every element is greater than 7 / 2 = 3.5. Similarly, the subarrays [6,5,6], [6,5,6,5], [6,5,6,5,8] also satisfy the given conditions. Therefore, 2, 3, 4, or 5 may also be returned.
Constraints:
1 <= nums.length <= 1051 <= nums[i], threshold <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Stack · Union-Find
[1,3,4,3,1] 6
[6,5,6,5,8] 7
maximum-subarray-min-product)smallest-k-length-subsequence-with-occurrences-of-a-letter)k-divisible-elements-subarrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2334: Subarray With Elements Greater Than Varying Threshold
class Solution {
private int[] p;
private int[] size;
public int validSubarraySize(int[] nums, int threshold) {
int n = nums.length;
p = new int[n];
size = new int[n];
for (int i = 0; i < n; ++i) {
p[i] = i;
size[i] = 1;
}
int[][] arr = new int[n][2];
for (int i = 0; i < n; ++i) {
arr[i][0] = nums[i];
arr[i][1] = i;
}
Arrays.sort(arr, (a, b) -> b[0] - a[0]);
boolean[] vis = new boolean[n];
for (int[] e : arr) {
int v = e[0], i = e[1];
if (i > 0 && vis[i - 1]) {
merge(i, i - 1);
}
if (i < n - 1 && vis[i + 1]) {
merge(i, i + 1);
}
if (v > threshold / size[find(i)]) {
return size[find(i)];
}
vis[i] = true;
}
return -1;
}
private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
private void merge(int a, int b) {
int pa = find(a), pb = find(b);
if (pa == pb) {
return;
}
p[pa] = pb;
size[pb] += size[pa];
}
}
// Accepted solution for LeetCode #2334: Subarray With Elements Greater Than Varying Threshold
func validSubarraySize(nums []int, threshold int) int {
n := len(nums)
p := make([]int, n)
size := make([]int, n)
for i := range p {
p[i] = i
size[i] = 1
}
var find func(int) int
find = func(x int) int {
if p[x] != x {
p[x] = find(p[x])
}
return p[x]
}
merge := func(a, b int) {
pa, pb := find(a), find(b)
if pa == pb {
return
}
p[pa] = pb
size[pb] += size[pa]
}
arr := make([][]int, n)
for i, v := range nums {
arr[i] = []int{v, i}
}
sort.Slice(arr, func(i, j int) bool {
return arr[i][0] > arr[j][0]
})
vis := make([]bool, n)
for _, e := range arr {
v, i := e[0], e[1]
if i > 0 && vis[i-1] {
merge(i, i-1)
}
if i < n-1 && vis[i+1] {
merge(i, i+1)
}
if v > threshold/size[find(i)] {
return size[find(i)]
}
vis[i] = true
}
return -1
}
# Accepted solution for LeetCode #2334: Subarray With Elements Greater Than Varying Threshold
class Solution:
def validSubarraySize(self, nums: List[int], threshold: int) -> int:
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
def merge(a, b):
pa, pb = find(a), find(b)
if pa == pb:
return
p[pa] = pb
size[pb] += size[pa]
n = len(nums)
p = list(range(n))
size = [1] * n
arr = sorted(zip(nums, range(n)), reverse=True)
vis = [False] * n
for v, i in arr:
if i and vis[i - 1]:
merge(i, i - 1)
if i < n - 1 and vis[i + 1]:
merge(i, i + 1)
if v > threshold // size[find(i)]:
return size[find(i)]
vis[i] = True
return -1
// Accepted solution for LeetCode #2334: Subarray With Elements Greater Than Varying Threshold
/**
* [2334] Subarray With Elements Greater Than Varying Threshold
*
* 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.
*
* Example 1:
*
* Input: nums = [1,3,4,3,1], threshold = 6
* Output: 3
* Explanation: The subarray [3,4,3] has a size of 3, and every element is greater than 6 / 3 = 2.
* Note that this is the only valid subarray.
*
* Example 2:
*
* Input: nums = [6,5,6,5,8], threshold = 7
* Output: 1
* Explanation: The subarray [8] has a size of 1, and 8 > 7 / 1 = 7. So 1 is returned.
* Note that the subarray [6,5] has a size of 2, and every element is greater than 7 / 2 = 3.5.
* Similarly, the subarrays [6,5,6], [6,5,6,5], [6,5,6,5,8] also satisfy the given conditions.
* Therefore, 2, 3, 4, or 5 may also be returned.
*
* Constraints:
*
* 1 <= nums.length <= 10^5
* 1 <= nums[i], threshold <= 10^9
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold/
// discuss: https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn valid_subarray_size(nums: Vec<i32>, threshold: i32) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2334_example_1() {
let nums = vec![1, 3, 4, 3, 1];
let threshold = 6;
let result = 3;
assert_eq!(Solution::valid_subarray_size(nums, threshold), result);
}
#[test]
#[ignore]
fn test_2334_example_2() {
let nums = vec![6, 5, 6, 5, 8];
let threshold = 7;
let result = 1;
assert_eq!(Solution::valid_subarray_size(nums, threshold), result);
}
}
// Accepted solution for LeetCode #2334: Subarray With Elements Greater Than Varying Threshold
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2334: Subarray With Elements Greater Than Varying Threshold
// class Solution {
// private int[] p;
// private int[] size;
//
// public int validSubarraySize(int[] nums, int threshold) {
// int n = nums.length;
// p = new int[n];
// size = new int[n];
// for (int i = 0; i < n; ++i) {
// p[i] = i;
// size[i] = 1;
// }
// int[][] arr = new int[n][2];
// for (int i = 0; i < n; ++i) {
// arr[i][0] = nums[i];
// arr[i][1] = i;
// }
// Arrays.sort(arr, (a, b) -> b[0] - a[0]);
// boolean[] vis = new boolean[n];
// for (int[] e : arr) {
// int v = e[0], i = e[1];
// if (i > 0 && vis[i - 1]) {
// merge(i, i - 1);
// }
// if (i < n - 1 && vis[i + 1]) {
// merge(i, i + 1);
// }
// if (v > threshold / size[find(i)]) {
// return size[find(i)];
// }
// vis[i] = true;
// }
// return -1;
// }
//
// private int find(int x) {
// if (p[x] != x) {
// p[x] = find(p[x]);
// }
// return p[x];
// }
//
// private void merge(int a, int b) {
// int pa = find(a), pb = find(b);
// if (pa == pb) {
// return;
// }
// p[pa] = pb;
// size[pb] += size[pa];
// }
// }
Use this to step through a reusable interview workflow for this problem.
For each element, scan left (or right) to find the next greater/smaller element. The inner scan can visit up to n elements per outer iteration, giving O(n²) total comparisons. No extra space needed beyond loop variables.
Each element is pushed onto the stack at most once and popped at most once, giving 2n total operations = O(n). The stack itself holds at most n elements in the worst case. The key insight: amortized O(1) per element despite the inner while-loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Pushing without popping stale elements invalidates next-greater/next-smaller logic.
Usually fails on: Indices point to blocked elements and outputs shift.
Fix: Pop while invariant is violated before pushing current element.