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
Maximum Removal Queries - DP Approach INPUT nums array 2 3 2 i=0 i=1 i=2 queries array 3 2 1 q[0] q[1] q[2] Goal: Process max queries by removing from ends if element >= query ALGORITHM STEPS 1 Define DP State dp[i][j] = max queries from subarray [i..j] 2 Process Query 0 q[0]=3: nums[1]=3 >= 3 Remove middle element 3 Process Query 1 q[1]=2: nums[0]=2 >= 2 Remove from left 4 Process Query 2 q[2]=1: nums[2]=2 >= 1 But only 1 elem left! Subsequence: [3, 2] 3 2 Optimal subsequence choice FINAL RESULT Query Processing Query 0: q=3 Remove 3 from [3,2] -- OK Query 1: q=2 Remove 2 from [2] -- OK Query 2: q=1 Array empty -- STOP Output 2 Maximum 2 queries processed successfully Key Insight: The DP approach considers all possible subsequences of nums and tracks the maximum queries processable. For each query, we try removing from either end if valid. The key is choosing the optimal subsequence that allows processing the most queries in order. Time complexity: O(n^2 * m) where n=len(nums), m=len(queries). TutorialsPoint - Maximum Number of Removal Queries That Can Be Processed I | DP Approach
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