Minimum changes required to make two arrays identical
Given two arrays
and
with n elements each. The task is to make these two arrays identical i:e, for each
, we want to make
. In a single operation, you can choose two integers x and y, and replace all the occurrences of x in both the arrays with y. Notice that regardless of the number of occurrences replaced, it will still be counted as a single operation. You have to output minimum number of operation required.
Examples:
Input : 1 2 2
1 2 5
Output: 1
Here, (x, y) = (5, 2) hence ans = 1.
Input : 2 1 1 3 5
1 2 2 4 5
Output: 2
Here, (x, y) = (1, 2) and (3, 4) thus ans = 2.
Other pairs are also possible.
This problem can be solved with the help of Disjoint Set Union.
We will check all elements of both the arrays i:e for each
. If the elements belong to same id then we skip it. Else, we do a Union operation on both elements. At last the answer will be the sum of the sizes of all the different disjoint sets formed i:e
. We subtract 1 because initially we take size of each set to be 1.
Below is the implementation of above approach :
C++
// C++ program to find minimum changes // required to make two arrays identical #include <bits/stdc++.h> using namespace std; #define N 100010 /* 'id': stores parent of a node. 'sz': stores size of a DSU tree. */int id[N], sz[N]; // Function to assign root int Root(int idx) { int i = idx; while (i != id[i]) id[i] = id[id[i]], i = id[i]; return i; } // Function to find Union void Union(int a, int b) { int i = Root(a), j = Root(b); if (i != j) { if (sz[i] >= sz[j]) { id[j] = i, sz[i] += sz[j]; sz[j] = 0; } else { id[i] = j, sz[j] += sz[i]; sz[i] = 0; } } } // function to find minimum changes required // to make both array equal. int minChange(int n, int a[], int b[]) { // Sets as single elements for (int i = 0; i < N; i++) id[i] = i, sz[i] = 1; // Combine items if they belong to different // sets. for (int i = 0; i < n; ++i) // true if both elements have different root if (Root(a[i]) != Root(b[i])) Union(a[i], b[i]); // make root equal // Find sum sizes of all sets formed. int ans = 0; for (int i = 0; i < n; ++i) if (id[i] == i) ans += (sz[i] - 1); return ans; } // Driver program int main() { int a[] = { 2, 1, 1, 3, 5 }, b[] = { 1, 2, 2, 4, 5 }; int n = sizeof(a) / sizeof(a[0]); cout << minChange(n, a, b); return 0; } |
Java
// Java program to find minimum changes // required to make two arrays identical class GFG{ static int N=100010; /* 'id': stores parent of a node. 'sz': stores size of a DSU tree. */static int[] id=new int[100010]; static int[] sz=new int[100010]; // Function to assign root static int Root(int idx) { int i = idx; while (i != id[i]) { id[i] = id[id[i]]; i = id[i]; } return i; } // Function to find Union static void Union(int a, int b) { int i = Root(a); int j = Root(b); if (i != j) { if (sz[i] >= sz[j]) { id[j] = i; sz[i] += sz[j]; sz[j] = 0; } else { id[i] = j; sz[j] += sz[i]; sz[i] = 0; } } } // function to find minimum changes required // to make both array equal. static int minChange(int n, int a[], int b[]) { // Sets as single elements for (int i = 0; i < N; i++) { id[i] = i; sz[i] = 1; } // Combine items if they belong to different // sets. for (int i = 0; i < n; ++i) // true if both elements have different root if (Root(a[i]) != Root(b[i])) Union(a[i], b[i]); // make root equal // Find sum sizes of all sets formed. int ans = 0; for (int i = 0; i < n; ++i) if (id[i] == i) ans += (sz[i] - 1); return ans; } // Driver program public static void main(String[] args) { int a[] = { 2, 1, 1, 3, 5 }, b[] = { 1, 2, 2, 4, 5 }; int n = a.length; System.out.println(minChange(n, a, b)); } } // This code is contributed by mits |
Python 3
# Python 3 program to find minimum changes # required to make two arrays identical N = 100010 # 'id':stores parent of a node # 'sz':stores size of a DSU tree ID = [0 for i in range(N)] sz = [0 for i in range(N)] # function to assign root def Root(idx): i = idx while i != ID[i]: ID[i], i = ID[ID[i]], ID[i] return i # Function to find Union def Union(a, b): i, j = Root(a), Root(b) if i != j: if sz[i] >= sz[j]: ID[j] = i sz[i] += sz[j] sz[j] = 0 else: ID[i] = j sz[j] += sz[i] sz[i] = 0 # function to find minimum changes # reqired to make both array equal def minChange(n, a, b): # sets as single elements for i in range(N): ID[i] = i sz[i] = 1 # Combine items if they belong # to differnet sets for i in range(n): # true if both elements have # different root if Root(a[i]) != Root(b[i]): Union(a[i], b[i]) # find sum sizes of all sets formed ans = 0 for i in range(n): if ID[i] == i: ans += (sz[i] - 1) return ans # Driver Code a = [2, 1, 1, 3, 5] b = [1, 2, 2, 4, 5] n = len(a) print(minChange(n, a, b)) # This code is contributed # by Mohit kumar 29 (IIIT gwalior) |
C#
// C# program to find minimum changes // required to make two arrays identical using System; class GFG{ static int N=100010; /* 'id': stores parent of a node. 'sz': stores size of a DSU tree. */static int []id=new int[100010]; static int []sz=new int[100010]; // Function to assign root static int Root(int idx) { int i = idx; while (i != id[i]) { id[i] = id[id[i]]; i = id[i]; } return i; } // Function to find Union static void Union(int a, int b) { int i = Root(a); int j = Root(b); if (i != j) { if (sz[i] >= sz[j]) { id[j] = i; sz[i] += sz[j]; sz[j] = 0; } else { id[i] = j; sz[j] += sz[i]; sz[i] = 0; } } } // function to find minimum changes required // to make both array equal. static int minChange(int n, int []a, int []b) { // Sets as single elements for (int i = 0; i < N; i++) { id[i] = i; sz[i] = 1; } // Combine items if they belong to different // sets. for (int i = 0; i < n; ++i) // true if both elements have different root if (Root(a[i]) != Root(b[i])) Union(a[i], b[i]); // make root equal // Find sum sizes of all sets formed. int ans = 0; for (int i = 0; i < n; ++i) if (id[i] == i) ans += (sz[i] - 1); return ans; } // Driver program public static void Main() { int []a = { 2, 1, 1, 3, 5 }; int []b = { 1, 2, 2, 4, 5 }; int n = a.Length; Console.WriteLine(minChange(n, a, b)); } } // This code is contributed by anuj_67.. |
PHP
= $sz[$j])
{
$id[$j] = $i;
$sz[$i] += $sz[$j];
$sz[$j] = 0;
}
else
{
$id[$i] = $j;
$sz[$j] += $sz[$i];
$sz[$i] = 0;
}
}
}
// function to find minimum changes
// required to make both array equal.
function minChange($n, &$a, &$b)
{
global $id, $sz, $N;
// Sets as single elements
for ($i = 0; $i < $N; $i++)
{
$id[$i] = $i;
$sz[$i] = 1;
}
// Combine items if they belong to
// different sets.
for ($i = 0; $i < $n; ++$i)
// true if both elements have
// different roots
if (Root($a[$i]) != Root($b[$i]))
Union($a[$i], $b[$i]); // make root equal
// Find sum sizes of all sets formed.
$ans = 0;
for ($i = 0; $i < $n; ++$i)
if ($id[$i] == $i)
$ans += ($sz[$i] - 1);
return $ans;
}
// Driver Code
$a = array(2, 1, 1, 3, 5);
$b = array(1, 2, 2, 4, 5);
$n = sizeof($a);
echo minChange($n, $a, $b);
// This code is contributed by ita_c
?>
2
Time Complexity: O(N + n) where N is maximum possible value of an array item and n is number of elements in the array.
Recommended Posts:
- Minimum swaps to make two arrays identical
- Minimum number of changes required to make the given array an AP
- Minimum number of given moves required to make N divisible by 25
- Minimum no. of operations required to make all Array Elements Zero
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to make all the array elements equal
- Minimum array insertions required to make consecutive difference <= K
- Minimum number of cuts required to make circle segments equal sized
- Find the minimum number of preprocess moves required to make two strings equal
- Operations required to make the string empty
- Find the number of operations required to make all array elements Equal
- Minimum Swaps required to group all 1's together
- Minimum boxes required to carry all gifts
- Minimum operations required to remove an array
- Minimum number of swaps required to sort an array | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



