K-th Smallest Remaining Even Integer in Subarray Queries Problem
Description
LeetCode Problem 3911.
You are given an integer array nums where nums is strictly increasing.
You are also given a 2D integer array queries, where queries[i] = [l_i, r_i, k_i].
For each query [l_i, r_i, k_i]:
- Consider the subarray
nums[l_i..r_i] - 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[l_i..r_i]. - Find the
k_i^thsmallest integer remaining in the sequence after the removals.
Return an integer array ans, where ans[i] is the result for the i^th 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[l_i..r_i] Removed Evens Remaining Evens k_i 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[l_i..r_i] Removed Evens Remaining Evens k_i 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[l_i..r_i] Removed Evens Remaining Evens k_i 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 <= 10^51 <= nums[i] <= 10^9numsis strictly increasing1 <= queries.length <= 10^5queries[i] = [l_i, r_i, k_i]0 <= l_i <= r_i < nums.length1 <= k_i <= 10^9
Solution
Approach: Prefix Counts + Binary Search
Only the even numbers in nums matter, since only they remove something from the sequence 2, 4, 6, .... Work with their halves: the question becomes finding the k-th positive integer that is not among the halves of the removed evens.
Collect the even values of nums in order and let h[j] be the j-th one divided by 2. Because nums is strictly increasing, h is strictly increasing, so h[j] - j never decreases. Let pre[i] be the number of even values among nums[0..i-1]. For a query [l, r, k], its removed halves are h[a..b-1] with a = pre[l] and b = pre[r + 1].
Before h[j], exactly h[j] - 1 - (j - a) positive integers are not removed. The answer is larger than h[j] as long as this count is below k. So binary search for the first j in [a, b) with h[j] - j >= k - a + 1 (or b if there is none) and call it p. Exactly p - a removed halves lie before the answer, which makes the answer's half equal to k + p - a, and the result is 2 * (k + p - a).
Complexity: time O((n + q) log n) and space O(n), where n is the length of nums and q the number of queries.
from bisect import bisect_left
class Solution:
def kthRemainingInteger(self, nums: List[int], queries: List[List[int]]) -> List[int]:
# e[j] = (j-th even value) / 2 - j, which never decreases
e = []
pre = [0]
for x in nums:
if x % 2 == 0:
e.append(x // 2 - len(e))
pre.append(len(e))
ans = []
for l, r, k in queries:
a, b = pre[l], pre[r + 1]
p = bisect_left(e, k - a + 1, a, b)
ans.append(2 * (k + p - a))
return ans