You are given a 0-indexed array of strings nums, where each string is of equal length and consists of only digits.

You are also given a 0-indexed 2D integer array queries where queries[i] = [ki, trimi]. For each queries[i], you need to:

  • Trim each number in nums to its rightmost trimi digits.
  • Determine the index of the ki-th smallest trimmed number in nums. If two trimmed numbers are equal, the number with the lower index is considered to be smaller.
  • Reset each number in nums to its original length.

Return an array answer of the same length as queries, where answer[i] is the answer to the i-th query.

Note:

  • To trim to the rightmost x digits means to keep removing the leftmost digit, until only x digits remain.
  • Strings in nums may contain leading zeros.

Input & Output

Example 1 — Basic Trimming
$ Input: nums = ["102","473","251","814"], queries = [[1,1],[2,3],[4,2],[1,2]]
Output: [2,2,1,0]
💡 Note: Query [1,1]: trim=1 gives ["2","3","1","4"], 1st smallest is "1" at index 2. Query [2,3]: trim=3 gives ["102","473","251","814"], 2nd smallest is "251" at index 2. Query [4,2]: trim=2 gives ["02","73","51","14"], 4th smallest is "73" at index 1. Query [1,2]: trim=2 gives ["02","73","51","14"], 1st smallest is "02" at index 0.
Example 2 — Leading Zeros
$ Input: nums = ["24","37","96","04"], queries = [[2,1],[2,2]]
Output: [3,0]
💡 Note: Query [2,1]: trim=1 gives ["4","7","6","4"], sorted: ["4","4","6","7"]. Second smallest "4" is at index 3 (lower index wins tiebreaker). Query [2,2]: trim=2 gives ["24","37","96","04"], sorted: ["04","24","37","96"]. Second smallest is "24" at index 0.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i].length ≤ 100
  • nums[i] consists of only digits
  • All nums[i] are of the same length
  • 1 ≤ queries.length ≤ 100
  • queries[i].length == 2
  • 1 ≤ ki ≤ nums.length
  • 1 ≤ trimi ≤ nums[i].length

Visualization

Tap to expand
Query Kth Smallest Trimmed Number INPUT nums array: "102" idx 0 "473" idx 1 "251" idx 2 "814" idx 3 queries [k, trim]: [1, 1] [2, 3] [4, 2] [1, 2] Example: trim=1 (last digit) 102 --> 2 (idx 0) 473 --> 3 (idx 1) 251 --> 1 (idx 2) 814 --> 4 (idx 3) Sorted: 1,2,3,4 (idx 2,0,1,3) k=1 smallest: idx 2 RADIX SORT APPROACH 1 Group Queries by Trim Sort queries by trim length Process in order: 1,2,2,3 2 Radix Sort Digit by Digit Start from rightmost digit Stable sort maintains order Buckets (0-9): 0 1 2 3 4 ... After digit 1: 251,102,473,814 After digit 2: 102,814,251,473 3 Answer Queries at Trim When trim matches query, pick k-th element index 4 Reuse Sorted State Extend sort for next trim No need to restart sorting Time: O(D*(N+10)) per digit FINAL RESULT Query Processing: Query [1,1]: k=1, trim=1 Trimmed: 2,3,1,4 --> Sorted: 1,2,3,4 1st smallest: idx 2 Query [2,3]: k=2, trim=3 Full: 102,473,251,814 2nd smallest: idx 2 (251) Query [4,2]: k=4, trim=2 Trimmed: 02,73,51,14 4th smallest: idx 1 (73) Query [1,2]: k=1, trim=2 Trimmed: 02,73,51,14 1st smallest: idx 0 (02) Output Array: [2, 2, 1, 0] OK - All queries answered Key Insight: Radix sort processes digits right-to-left, naturally aligning with trimming from the right. By grouping queries by trim length and sorting them, we can answer multiple queries at the same trim level without re-sorting. The stable nature of radix sort ensures ties are broken by original index, matching the problem requirement. TutorialsPoint - Query Kth Smallest Trimmed Number | Radix Sort Approach
Asked in
Google 15 Amazon 12 Microsoft 8
28.5K Views
Medium Frequency
~25 min Avg. Time
847 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