Sort the Jumbled Numbers - Problem

You are given a 0-indexed integer array mapping which represents the mapping rule of a shuffled decimal system. mapping[i] = j means digit i should be mapped to digit j in this system.

The mapped value of an integer is the new integer obtained by replacing each occurrence of digit i in the integer with mapping[i] for all 0 <= i <= 9.

You are also given another integer array nums. Return the array nums sorted in non-decreasing order based on the mapped values of its elements.

Note: Elements with the same mapped values should appear in the same relative order as in the input.

Input & Output

Example 1 — Basic Case
$ Input: mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]
Output: [338,38,991]
💡 Note: 991 maps to 669, 338 maps to 007 (which is 7), 38 maps to 07 (which is 7). Since 7 < 669, both 338 and 38 come before 991. Since 338 and 38 have the same mapped value, they maintain their original relative order.
Example 2 — Different Mapping
$ Input: mapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123]
Output: [123,456,789]
💡 Note: With identity mapping, numbers stay the same: 789→789, 456→456, 123→123. Sorted: [123,456,789].
Example 3 — All Same Mapped Value
$ Input: mapping = [1,1,1,1,1,1,1,1,1,1], nums = [12,21,3]
Output: [3,12,21]
💡 Note: All numbers map using only 1s: 12→11, 21→11, 3→1. Sorted by mapped values: 1 < 11, so 3 comes first. Since 12 and 21 both map to 11, they maintain their original relative order, giving [3,12,21].

Constraints

  • mapping.length == 10
  • 0 ≤ mapping[i] ≤ 9
  • All the values of mapping[i] are unique
  • 1 ≤ nums.length ≤ 3 × 104
  • 0 ≤ nums[i] < 109

Visualization

Tap to expand
Sort the Jumbled Numbers INPUT mapping[] array idx: 0 1 2 3 4 5 6 7 8 9 val: 8 9 4 0 2 1 3 5 7 6 nums[] array 991 338 38 Digit i maps to mapping[i] Example: digit 9 maps to 6 Mapping Examples: 9 --> 6 3 --> 0 8 --> 7 1 --> 9 0 --> 8 ALGORITHM STEPS 1 Create Pairs (mapped_value, index) 991: 9->6, 9->6, 1->9 = 669 338: 3->0, 3->0, 8->7 = 007 38: 3->0, 8->7 = 07 2 Store Pairs (mapped, original_idx) [(669, 0), (7, 1), (7, 2)] nums[0]=991, nums[1]=338, nums[2]=38 3 Stable Sort by mapped values Sorted: [(7,1), (7,2), (669,0)] 338, 38 have same mapped value 4 Extract Result using stored indices result = [nums[1], nums[2], nums[0]] FINAL RESULT Sorted Array 338 38 991 Mapped Values: 7 7 669 7 <= 7 < 669 Stable Sort Preserved! 338 and 38 both map to 7 338 comes before 38 in input Same order kept in output Output: [338, 38, 991] Key Insight: Use stable sort to maintain relative order of elements with equal mapped values. Store pairs of (mapped_value, original_index) and sort by mapped_value. Elements with same mapped value preserve their original order. Time: O(n * d * log n) where d = max digits, Space: O(n) TutorialsPoint - Sort the Jumbled Numbers | Optimized with Stable Sort
Asked in
Google 12 Amazon 8 Microsoft 6
15.2K Views
Medium Frequency
~15 min Avg. Time
423 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