Maximum Number of Removal Queries That Can Be Processed I - Problem
The Query Processing Challenge

You're given two arrays: nums (your data array) and queries (processing requests). Your goal is to maximize the number of queries you can successfully process.

🎯 The Rules:
One-time preprocessing: You can replace nums with any subsequence of itself (keep elements in original order)
Query processing: Process queries in order. For each query queries[i]:
  - If both first and last elements of nums are less than queries[i], processing stops
  - Otherwise, remove either the first or last element (whichever is ≥ queries[i])

Challenge: Choose the optimal subsequence and removal strategy to process the maximum number of queries!

Example: nums = [2, 3, 2], queries = [3, 2, 1]
→ Keep subsequence [3, 2]
→ Process query 3: remove 3, left with [2]
→ Process query 2: remove 2, left with []
→ Query 1: no elements left, but we processed 2 queries!

Input & Output

example_1.py — Basic Case
$ Input: {"nums": [2, 3, 2], "queries": [3, 2, 1]}
Output: 2
💡 Note: Choose subsequence [3, 2]. Process query 3 by removing 3 (count=1), then process query 2 by removing 2 (count=2). Cannot process query 1 as no elements remain.
example_2.py — All Elements Work
$ Input: {"nums": [5, 4, 3], "queries": [3, 4, 1]}
Output: 3
💡 Note: Keep the full array [5, 4, 3]. Process query 3: remove 3 from right. Process query 4: remove 4. Process query 1: remove 5. All queries processed successfully.
example_3.py — Strategic Subsequence
$ Input: {"nums": [1, 100, 1], "queries": [90, 90]}
Output: 1
💡 Note: Choose subsequence [100]. Can process first query 90 by removing 100, but then no elements remain for the second query. Maximum achievable is 1.

Constraints

  • 1 ≤ nums.length ≤ 20
  • 1 ≤ queries.length ≤ 1000
  • 1 ≤ nums[i], queries[i] ≤ 106
  • The subsequence must maintain the original order of elements

Visualization

Tap to expand
Query Processing StrategyOriginal Array:232Optimal Subsequence [3, 2]:32LRQuery Processing:Query 3: Remove 3 (left) ✓ Count = 1Query 2: Remove 2 (right) ✓ Count = 2Query 1: No elements left ✗ Final = 2Why This Works:• We can only remove from ends• Contiguous intervals are optimal• Greedy end selection works• O(n²m) vs O(2ⁿm) complexity💡
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
25.8K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen