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.
Move from brute-force thinking to an efficient approach using array strategy.
Given an integer array nums containing n integers, find the beauty of each subarray of size k.
The beauty of a subarray is the xth smallest integer in the subarray if it is negative, or 0 if there are fewer than x negative integers.
Return an integer array containing n - k + 1 integers, which denote the beauty of the subarrays in order from the first index in the array.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,-1,-3,-2,3], k = 3, x = 2 Output: [-1,-2,-2] Explanation: There are 3 subarrays with size k = 3. The first subarray is[1, -1, -3]and the 2nd smallest negative integer is -1. The second subarray is[-1, -3, -2]and the 2nd smallest negative integer is -2. The third subarray is[-3, -2, 3]and the 2nd smallest negative integer is -2.
Example 2:
Input: nums = [-1,-2,-3,-4,-5], k = 2, x = 2 Output: [-1,-2,-3,-4] Explanation: There are 4 subarrays with size k = 2. For[-1, -2], the 2nd smallest negative integer is -1. For[-2, -3], the 2nd smallest negative integer is -2. For[-3, -4], the 2nd smallest negative integer is -3. For[-4, -5], the 2nd smallest negative integer is -4.
Example 3:
Input: nums = [-3,1,2,-3,0,-3], k = 2, x = 1 Output: [-3,0,-3,-3,-3] Explanation: There are 5 subarrays with size k = 2. For[-3, 1], the 1st smallest negative integer is -3. For[1, 2], there is no negative integer so the beauty is 0. For[2, -3], the 1st smallest negative integer is -3. For[-3, 0], the 1st smallest negative integer is -3. For[0, -3], the 1st smallest negative integer is -3.
Constraints:
n == nums.length 1 <= n <= 1051 <= k <= n1 <= x <= k -50 <= nums[i] <= 50 Problem summary: Given an integer array nums containing n integers, find the beauty of each subarray of size k. The beauty of a subarray is the xth smallest integer in the subarray if it is negative, or 0 if there are fewer than x negative integers. Return an integer array containing n - k + 1 integers, which denote the beauty of the subarrays in order from the first index in the array. 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 · Hash Map · Sliding Window
[1,-1,-3,-2,3] 3 2
[-1,-2,-3,-4,-5] 2 2
[-3,1,2,-3,0,-3] 2 1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2653: Sliding Subarray Beauty
class Solution {
public int[] getSubarrayBeauty(int[] nums, int k, int x) {
int n = nums.length;
int[] cnt = new int[101];
for (int i = 0; i < k; ++i) {
++cnt[nums[i] + 50];
}
int[] ans = new int[n - k + 1];
ans[0] = f(cnt, x);
for (int i = k, j = 1; i < n; ++i) {
++cnt[nums[i] + 50];
--cnt[nums[i - k] + 50];
ans[j++] = f(cnt, x);
}
return ans;
}
private int f(int[] cnt, int x) {
int s = 0;
for (int i = 0; i < 50; ++i) {
s += cnt[i];
if (s >= x) {
return i - 50;
}
}
return 0;
}
}
// Accepted solution for LeetCode #2653: Sliding Subarray Beauty
func getSubarrayBeauty(nums []int, k int, x int) []int {
n := len(nums)
cnt := [101]int{}
for _, x := range nums[:k] {
cnt[x+50]++
}
ans := make([]int, n-k+1)
f := func(x int) int {
s := 0
for i := 0; i < 50; i++ {
s += cnt[i]
if s >= x {
return i - 50
}
}
return 0
}
ans[0] = f(x)
for i, j := k, 1; i < n; i, j = i+1, j+1 {
cnt[nums[i]+50]++
cnt[nums[i-k]+50]--
ans[j] = f(x)
}
return ans
}
# Accepted solution for LeetCode #2653: Sliding Subarray Beauty
class Solution:
def getSubarrayBeauty(self, nums: List[int], k: int, x: int) -> List[int]:
def f(x: int) -> int:
s = 0
for i in range(50):
s += cnt[i]
if s >= x:
return i - 50
return 0
cnt = [0] * 101
for v in nums[:k]:
cnt[v + 50] += 1
ans = [f(x)]
for i in range(k, len(nums)):
cnt[nums[i] + 50] += 1
cnt[nums[i - k] + 50] -= 1
ans.append(f(x))
return ans
// Accepted solution for LeetCode #2653: Sliding Subarray Beauty
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2653: Sliding Subarray Beauty
// class Solution {
// public int[] getSubarrayBeauty(int[] nums, int k, int x) {
// int n = nums.length;
// int[] cnt = new int[101];
// for (int i = 0; i < k; ++i) {
// ++cnt[nums[i] + 50];
// }
// int[] ans = new int[n - k + 1];
// ans[0] = f(cnt, x);
// for (int i = k, j = 1; i < n; ++i) {
// ++cnt[nums[i] + 50];
// --cnt[nums[i - k] + 50];
// ans[j++] = f(cnt, x);
// }
// return ans;
// }
//
// private int f(int[] cnt, int x) {
// int s = 0;
// for (int i = 0; i < 50; ++i) {
// s += cnt[i];
// if (s >= x) {
// return i - 50;
// }
// }
// return 0;
// }
// }
// Accepted solution for LeetCode #2653: Sliding Subarray Beauty
function getSubarrayBeauty(nums: number[], k: number, x: number): number[] {
const n = nums.length;
const cnt: number[] = new Array(101).fill(0);
for (let i = 0; i < k; ++i) {
++cnt[nums[i] + 50];
}
const ans: number[] = new Array(n - k + 1);
const f = (x: number): number => {
let s = 0;
for (let i = 0; i < 50; ++i) {
s += cnt[i];
if (s >= x) {
return i - 50;
}
}
return 0;
};
ans[0] = f(x);
for (let i = k, j = 1; i < n; ++i, ++j) {
cnt[nums[i] + 50]++;
cnt[nums[i - k] + 50]--;
ans[j] = f(x);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each starting index, scan the next k elements to compute the window aggregate. There are n−k+1 starting positions, each requiring O(k) work, giving O(n × k) total. No extra space since we recompute from scratch each time.
The window expands and contracts as we scan left to right. Each element enters the window at most once and leaves at most once, giving 2n total operations = O(n). Space depends on what we track inside the window (a hash map of at most k distinct elements, or O(1) for a fixed-size window).
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: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.