Skip to main content

K-th Smallest Remaining Even Integer in Subarray Queries - Solution & Explanation

HardArrayBinary Search4 min read
Practice this problem

Problem Statement

You are given an integer array nums where nums is strictly increasing.

You are also given a 2D integer array queries, where queries[i] = [li, ri, ki].

For each query [li, ri, ki]:

  • Consider the subarray nums[li..ri]
  • From the infinite sequence of all positive even integers: 2, 4, 6, 8, 10, 12, 14, ...
  • Remove all elements that appear in the subarray nums[li..ri].
  • Find the kith smallest integer remaining in the sequence after the removals.

Return an integer array ans, where ans[i] is the result for the ith query.

 

Example 1:

Input: nums = [1,4,7], queries = [[0,2,1],[1,1,2],[0,0,3]]

Output: [2,6,6]

Explanation:

i queries[i] nums[li..ri] Removed
Evens
Remaining
Evens
ki ans[i]
0 [0, 2, 1] [1, 4, 7] [4] 2, 6, 8, ... 1 2
1 [1, 1, 2] [4] [4] 2, 6, 8, ... 2 6
2 [0, 0, 3] [1] [] 2, 4, 6, ... 3 6

Thus, ans = [2, 6, 6].

Example 2:

Input: nums = [2,5,8], queries = [[0,1,2],[1,2,1],[0,2,4]]

Output: [6,2,12]

Explanation:

i queries[i] nums[li..ri] Removed
Evens
Remaining
Evens
ki ans[i]
0 [0, 1, 2] [2, 5] [2] 4, 6, 8, ... 2 6
1 [1, 2, 1] [5, 8] [8] 2, 4, 6, ... 1 2
2 [0, 2, 4] [2, 5, 8] [2, 8] 4, 6, 10, 12, ... 4 12

Thus, ans = [6, 2, 12].

Example 3:

Input: nums = [3,6], queries = [[0,1,1],[1,1,3]]

Output: [2,8]

Explanation:

i queries[i] nums[li..ri] Removed
Evens
Remaining
Evens
ki ans[i]
0 [0, 1, 1] [3, 6] [6] 2, 4, 8, ... 1 2
1 [1, 1, 3] [6] [6] 2, 4, 8, ... 3 8

Thus, ans = [2, 8].

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109
  • nums is strictly increasing
  • 1 <= queries.length <= 105
  • queries[i] = [li, ri, ki]
  • 0 <= li <= ri < nums.length
  • 1 <= ki <= 109​​​​​​​

Approach Overview

Problem Overview: You receive an array and multiple queries [l, r, k]. For each query, consider the elements inside the subarray and keep only the even numbers. From those remaining values, return the k-th smallest element. If fewer than k even numbers exist, return an indicator such as -1.

Approach 1: Filter + Sort Per Query (Brute Force) (Time: O(q * n log n), Space: O(n))

The direct method iterates through every query and scans the range [l, r]. Collect only even numbers into a temporary list, sort that list, and return the k-th element if it exists. The logic is simple: iterate through the subarray, apply a modulus check num % 2 == 0, then sort. This approach works for small inputs but becomes slow when the array and query count grow because each query repeats the same scanning and sorting work.

Approach 2: Pre-filter Evens + Binary Search on Positions (Time: O(q log n + m log m), Space: O(m))

Store only the even numbers along with their indices. Maintain two arrays: positions and values. For a query [l, r], use binary search to find the first and last even indices that fall inside the range. Extract the corresponding values and determine the k-th smallest. Sorting each subset still costs time, but the search space shrinks significantly when the array contains many odd numbers. This technique combines index filtering with binary search.

Approach 3: Merge Sort Tree / Range Order Statistics (Time: O((n + q) log² n), Space: O(n log n))

Build a segment tree where each node stores a sorted list of the even numbers present in that segment. Odd values are ignored during construction. To answer a query, traverse the segment tree nodes covering [l, r]. Instead of merging arrays at query time, perform a value-based binary search: guess a candidate value and count how many even numbers ≤ that value exist across the relevant nodes. Each count query uses binary search inside the node lists, producing O(log² n) per query. This technique effectively turns the problem into a range order-statistic query.

Approach 4: Wavelet Tree / Order Statistic Structure (Time: O((n + q) log n), Space: O(n log n))

A wavelet tree or advanced order statistic tree can directly answer "k-th smallest in range" queries. Build the structure using only even values (or mark odds so they are skipped). Each query walks down the tree while maintaining rank counts inside [l, r]. This reduces the query complexity to roughly O(log n), making it the fastest scalable solution for large inputs.

Recommended for interviews: Start by describing the brute-force scan and sort approach to show understanding of the query requirement. Then move quickly to the segment tree or merge sort tree solution. Interviewers usually expect a range order-statistic structure because it demonstrates familiarity with range queries, binary search on answer, and advanced data structures.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Filter + Sort Per QueryO(q * n log n)O(n)Small arrays or when query count is very low
Pre-filter Evens + Binary SearchO(q log n + m log m)O(m)When many numbers are odd and filtering greatly reduces the dataset
Merge Sort TreeO((n + q) log² n)O(n log n)General case with many range queries
Wavelet Tree / Order Statistic TreeO((n + q) log n)O(n log n)Large datasets where fast k-th element queries are required

Video Solution

Leetcode shocks💀everyone by Double BinarySearch Problem in Biweekly Contest 181 , Q4(3911 Explained)Kumar K [Amazon]780 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is K-th Smallest Remaining Even Integer in Subarray Queries easy or hard?
The problem is typically categorized as Hard because it combines range queries with order statistics and filtering conditions. Efficient solutions require knowledge of segment trees, binary search on the answer, or advanced structures such as wavelet trees.
K-th Smallest Remaining Even Integer in Subarray Queries Python/Java solution
In Python or Java, the usual implementation builds a merge sort tree where each node stores a sorted list of even numbers from its segment. Queries traverse relevant nodes and perform binary searches inside their lists to count elements and locate the k-th smallest candidate.
How to solve K-th Smallest Remaining Even Integer in Subarray Queries in O(n)?
Pure O(n) per query is generally not feasible because the task requires both range filtering and order statistics. The closest practical optimization is preprocessing with advanced structures like wavelet trees, giving O(log n) query time after O(n log n) build time.
What is the best approach for K-th Smallest Remaining Even Integer in Subarray Queries?
The most practical approach uses a merge sort tree or another range order-statistic structure. Each node stores sorted even values for its segment, allowing queries to count how many elements are ≤ a candidate value. Combined with binary search on the answer, each query runs in O(log² n) time while preprocessing takes O(n log n).
Is K-th Smallest Remaining Even Integer in Subarray Queries asked at Google/Amazon/Meta?
Variants of this problem appear in interviews at companies like Google, Amazon, and Meta under range query or order-statistic themes. Interviewers often frame it as 'k-th smallest number in a subarray' with extra constraints such as filtering by parity.
What data structure is used in K-th Smallest Remaining Even Integer in Subarray Queries?
Typical solutions rely on segment trees, merge sort trees, wavelet trees, or order-statistic balanced trees. These structures support efficient range queries and rank counting, which are required to locate the k-th smallest value among filtered elements.
What is the time complexity of K-th Smallest Remaining Even Integer in Subarray Queries?
Brute force scanning and sorting per query costs O(q * n log n). Optimized solutions using merge sort trees handle each query in O(log² n) after O(n log n) preprocessing. Wavelet trees or specialized order-statistic structures can reduce query time to about O(log n).

Ready to solve this problem?

Practice K-th Smallest Remaining Even Integer in Subarray Queries with our built-in code editor and test cases.

Practice on FleetCode