Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to minimize hamming distance after swap operations in Python
The Hamming distance between two arrays is the number of positions where elements differ. This problem asks us to minimize the Hamming distance between two arrays src and tgt by performing allowed swap operations on the source array.
The key insight is that allowed swaps create connected components of indices. Within each component, we can rearrange elements optimally to match the target array as much as possible.
Algorithm Overview
We use a Union-Find (Disjoint Set) data structure to group indices that can be swapped with each other. For each group, we count mismatches and calculate the minimum Hamming distance.
Step-by-Step Approach
- Create a Union-Find structure to group swappable indices
- For each group, count frequency differences between source and target elements
- Calculate minimum mismatches for each group
- Sum up all group mismatches
Example
Let's trace through the example where src = [2,3,4,5], tgt = [3,2,5,6], and allowedSwaps = [[0,1],[2,3]] ?
from collections import defaultdict, Counter
def solve(src, tgt, allowedSwaps):
# Initialize Union-Find structure
graph = [n for n in range(len(src))]
def find(x):
# Path compression for efficiency
while graph[x] != x:
graph[x] = graph[graph[x]]
x = graph[x]
return x
def union(x, y):
# Union two components
x1, y1 = find(x), find(y)
graph[x1] = y1
# Build connected components from allowed swaps
for x, y in allowedSwaps:
union(x, y)
# Group indices by their root component
groups = defaultdict(list)
for i in range(len(src)):
root = find(i)
groups[root].append(i)
# Calculate minimum Hamming distance
ans = 0
for indices in groups.values():
counter = Counter()
for idx in indices:
counter[src[idx]] += 1 # Count source elements
counter[tgt[idx]] -= 1 # Subtract target elements
# Sum absolute differences and divide by 2
ans += sum(abs(val) for val in counter.values()) // 2
return ans
# Test example
src = [2, 3, 4, 5]
tgt = [3, 2, 5, 6]
allowedSwaps = [[0, 1], [2, 3]]
result = solve(src, tgt, allowedSwaps)
print(f"Minimum Hamming distance: {result}")
Minimum Hamming distance: 1
How It Works
The algorithm creates two connected components:
- Component 1: Indices [0, 1] with elements [2, 3] in src and [3, 2] in tgt
- Component 2: Indices [2, 3] with elements [4, 5] in src and [5, 6] in tgt
For Component 1: Elements match perfectly after optimal swapping (contribution = 0).
For Component 2: One element (6) in target has no match in source (contribution = 1).
Complexity Analysis
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(n + m) | n = array length, m = number of swaps |
| Space | O(n) | Union-Find structure and grouping |
Conclusion
This solution uses Union-Find to group swappable indices and calculates optimal element matching within each group. The algorithm efficiently finds the minimum possible Hamming distance in linear time.
