Minimize Hamming Distance After Swap Operations - Problem

Imagine you have two arrays of the same length - source and target - and you want to make them as similar as possible! ๐ŸŽฏ

You're given a set of allowed swap operations that let you swap elements at specific pairs of indices in the source array. The catch? You can perform these swaps multiple times and in any order!

Your goal is to minimize the Hamming distance - the number of positions where the arrays differ. Think of it as a puzzle where you need to rearrange pieces optimally to match a target pattern.

Example: If source = [1,2,3,4] and target = [2,1,4,5], and you can swap positions (0,1) and (2,3), you could swap elements at indices 0 and 1 to get [2,1,3,4], reducing differences from 4 positions to just 2!

Input & Output

example_1.py โ€” Basic Swapping
$ Input: source = [1,2,3,4], target = [2,1,4,5], allowedSwaps = [[0,1],[2,3]]
โ€บ Output: 1
๐Ÿ’ก Note: We can swap indices (0,1) to get [2,1,3,4]. This matches target at positions 0,1. Position 2 differs (3โ‰ 4) and position 3 differs (4โ‰ 5), but we can swap (2,3) to get [2,1,4,3]. Now positions 0,1,2 match but position 3 still differs (3โ‰ 5). Final distance is 1.
example_2.py โ€” No Swaps Needed
$ Input: source = [1,2,3,4], target = [1,2,3,4], allowedSwaps = [[0,1],[2,3]]
โ€บ Output: 0
๐Ÿ’ก Note: Arrays are already identical, so Hamming distance is 0. No swaps needed.
example_3.py โ€” Complete Mismatch
$ Input: source = [5,6,7,8], target = [1,2,3,4], allowedSwaps = [[0,1],[2,3]]
โ€บ Output: 4
๐Ÿ’ก Note: No element in source matches any element in target, regardless of swaps. All 4 positions will differ, so minimum Hamming distance is 4.

Visualization

Tap to expand
Union-Find: Optimize Within Connected GroupsGroup 1: Indices {0,1,2}idx 0src:1tgt:3idx 1src:2tgt:1idx 2src:3tgt:2src freq: {1:1, 2:1, 3:1}tgt freq: {1:1, 2:1, 3:1}matches: 3Group 2: Indices {3,4}idx 3src:4tgt:5idx 4src:5tgt:4src freq: {4:1, 5:1}tgt freq: {4:1, 5:1}matches: 2Final CalculationTotal matches: 3 + 2 = 5Hamming distance: 5 - 5 = 0๐ŸŽฏ Key insight: Elements in the same connected component can be rearranged optimally!
Understanding the Visualization
1
Build Groups
Use Union-Find to identify which indices can reach each other through swaps
2
Analyze Each Group
For each connected component, count how many of each value appear in source and target
3
Optimize Matching
Within each group, match as many source values with target values as possible
4
Calculate Result
Sum up all possible matches and subtract from total length to get minimum distance
Key Takeaway
๐ŸŽฏ Key Insight: By identifying connected components through Union-Find, we can optimally arrange elements within each group independently, maximizing matches and minimizing the Hamming distance.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^m ร— n)

Where m is number of allowed swaps and n is array length. We explore exponentially many swap combinations.

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for storing current array state and recursion stack

n
2n
โšก Linearithmic Space

Constraints

  • n == source.length == target.length
  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค source[i], target[i] โ‰ค 105
  • 0 โ‰ค allowedSwaps.length โ‰ค 105
  • allowedSwaps[i].length == 2
  • 0 โ‰ค ai, bi โ‰ค n - 1
  • ai โ‰  bi
Asked in
Google 25 Amazon 18 Meta 12 Microsoft 8
23.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