Replace Elements in an Array - Problem

You are given a 0-indexed array nums consisting of n distinct positive integers. Your task is to perform a series of replacement operations that will transform this array step by step.

You need to apply m operations to this array, where each operation is defined by a pair [oldValue, newValue]. In the i-th operation, you must:

  • Find the element operations[i][0] in the array
  • Replace it with operations[i][1]

Important guarantees:

  • 🎯 operations[i][0] always exists in the current array
  • 🎯 operations[i][1] never exists in the current array before replacement

Return the final array after applying all operations in sequence.

Example: If nums = [1,2,4,6] and operations = [[1,3],[4,7],[6,1]]:
β€’ Replace 1 β†’ 3: [3,2,4,6]
β€’ Replace 4 β†’ 7: [3,2,7,6]
β€’ Replace 6 β†’ 1: [3,2,7,1]

Input & Output

example_1.py β€” Basic Replacement
$ Input: nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]]
β€Ί Output: [3,2,7,1]
πŸ’‘ Note: Step by step: [1,2,4,6] β†’ replace 1 with 3 β†’ [3,2,4,6] β†’ replace 4 with 7 β†’ [3,2,7,6] β†’ replace 6 with 1 β†’ [3,2,7,1]
example_2.py β€” Chain Replacements
$ Input: nums = [1,2], operations = [[1,3],[2,1],[3,2]]
β€Ί Output: [2,1]
πŸ’‘ Note: Operations create a cycle: [1,2] β†’ [3,2] β†’ [3,1] β†’ [2,1]. Notice how values can be reused after being replaced.
example_3.py β€” Single Element
$ Input: nums = [5], operations = [[5,10]]
β€Ί Output: [10]
πŸ’‘ Note: Edge case with single element: simply replace 5 with 10.

Constraints

  • 1 ≀ nums.length ≀ 105
  • 1 ≀ nums[i], operations[i][1] ≀ 106
  • 0 ≀ operations.length ≀ 105
  • operations[i].length == 2
  • All values in nums are distinct
  • operations[i][0] exists in nums before operation i
  • operations[i][1] does not exist in nums before operation i

Visualization

Tap to expand
πŸ“š Library Book Management SystemInitial Bookshelf:Book1Shelf 0Book2Shelf 1Book4Shelf 2Book6Shelf 3πŸ“– Catalog (Hash Map):Book 1 β†’ Shelf 0Book 2 β†’ Shelf 1Book 4 β†’ Shelf 2Book 6 β†’ Shelf 3πŸ”„ Operation: Replace Book 1 β†’ Book 3⚑ Catalog lookup: Book 1 is on Shelf 0 (O(1) time!)Book3NEW!Book2Book4Book6Instant Lookup!πŸ“– Updated Catalog:Book 3 β†’ Shelf 0Book 2 β†’ Shelf 1Book 4 β†’ Shelf 2Book 6 β†’ Shelf 3Catalog stays in sync!πŸ’‘ Key Insight:Instead of searching every shelf for each book replacement (O(nΓ—m)), maintain a catalog for instant lookups (O(n+m))!
Understanding the Visualization
1
Initial Setup
Start with books [1,2,4,6] on shelves 0,1,2,3. Create catalog: {1β†’shelf0, 2β†’shelf1, 4β†’shelf2, 6β†’shelf3}
2
First Replacement
Replace book 1 with book 3: Look up book 1 in catalog (shelf 0), swap it with book 3, update catalog
3
Continue Efficiently
Each subsequent replacement uses the catalog for instant lookup instead of searching every shelf
4
Final Result
After all replacements: [3,2,7,1] with updated catalog maintaining accuracy
Key Takeaway
🎯 Key Insight: By maintaining a catalog (hash map) of book locations, we transform expensive shelf-by-shelf searches into instant lookups, making the library management system highly efficient for multiple book replacements!
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
48.0K Views
Medium Frequency
~15 min Avg. Time
1.4K 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