Return the Negative x-th Order Statistic of Every Fixed Window

Quick Overview

For each length-k window, return its x-th smallest value when negative and zero otherwise, preserving window order and duplicate multiplicities.

Return the Negative x-th Order Statistic of Every Fixed Window

Company: Infosys

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem For each contiguous length-k window in nums, its beauty is its x-th smallest value if that value is negative; otherwise its beauty is zero. Return beauties from leftmost window to rightmost, counting duplicate entries as separate positions. ```cpp class Solution { public: vector<int> getSubarrayBeauty(vector<int>& nums, int k, int x); }; ``` ### Examples **Example 1:** ```text Input: nums = [-2,4,-1,-3], k = 3, x = 2 Output: [-1,-1] Explanation: The sorted windows are [-2,-1,4] and [-3,-1,4]; each has -1 in position two. ``` **Example 2:** ```text Input: nums = [0,-1,2], k = 2, x = 2 Output: [0,0] Explanation: Each window contains fewer than two negative entries. ``` ### Constraints - `n == nums.length`; `1 <= n <= 100000`. - `1 <= k <= n`; `1 <= x <= k`. - `-50 <= nums[i] <= 50`. ### Clarifying Questions - Is x one-based? Yes; `x = 1` asks for the smallest value. - Do duplicates occupy separate sorted positions? Yes. - Is zero negative? No. - What if there are fewer than x negatives? Return zero for that window. - What order is required? Leftmost window first. ```hint Check the boundary Check a window whose x-th smallest value is zero. ```

Overview: For each length-k window, return its x-th smallest value when negative and zero otherwise, preserving window order and duplicate multiplicities.

Infosys logo
Infosys
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

For each contiguous length-k window in nums, its beauty is its x-th smallest value if that value is negative; otherwise its beauty is zero. Return beauties from leftmost window to rightmost, counting duplicate entries as separate positions.

class Solution {
public:
    vector<int> getSubarrayBeauty(vector<int>& nums, int k, int x);
};

Examples

Example 1:

Input: nums = [-2,4,-1,-3], k = 3, x = 2
Output: [-1,-1]
Explanation: The sorted windows are [-2,-1,4] and [-3,-1,4]; each has -1 in position two.

Example 2:

Input: nums = [0,-1,2], k = 2, x = 2
Output: [0,0]
Explanation: Each window contains fewer than two negative entries.

Constraints

  • n == nums.length ; 1 <= n <= 100000 .
  • 1 <= k <= n ; 1 <= x <= k .
  • -50 <= nums[i] <= 50 .

Clarifying Questions Guidance

  • Is x one-based? Yes; x = 1 asks for the smallest value.
  • Do duplicates occupy separate sorted positions? Yes.
  • Is zero negative? No.
  • What if there are fewer than x negatives? Return zero for that window.
  • What order is required? Leftmost window first.