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 the minimum number of operations 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 arrays i:e for each . If the elements belong to the same id then we skip it. Otherwise, we do a Union operation on both elements. Finally, 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 the size of each set to be 1.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 100010
int id[N], sz[N];
int Root( int idx)
{
int i = idx;
while (i != id[i])
id[i] = id[id[i]], i = id[i];
return i;
}
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;
}
}
}
int minChange( int n, int a[], int b[])
{
for ( int i = 0; i < N; i++)
id[i] = i, sz[i] = 1;
for ( int i = 0; i < n; ++i)
if (Root(a[i]) != Root(b[i]))
Union(a[i], b[i]);
int ans = 0;
for ( int i = 0; i < n; ++i)
if (id[i] == i)
ans += (sz[i] - 1);
return ans;
}
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
class GFG{
static int N= 100010 ;
static int [] id= new int [ 100010 ];
static int [] sz= new int [ 100010 ];
static int Root( int idx)
{
int i = idx;
while (i != id[i])
{
id[i] = id[id[i]];
i = id[i];
}
return i;
}
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 ;
}
}
}
static int minChange( int n, int a[], int b[])
{
for ( int i = 0 ; i < N; i++)
{
id[i] = i;
sz[i] = 1 ;
}
for ( int i = 0 ; i < n; ++i)
if (Root(a[i]) != Root(b[i]))
Union(a[i], b[i]);
int ans = 0 ;
for ( int i = 0 ; i < n; ++i)
if (id[i] == i)
ans += (sz[i] - 1 );
return ans;
}
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));
}
}
|
Python3
N = 100010
ID = [ 0 for i in range (N)]
sz = [ 0 for i in range (N)]
def Root(idx):
i = idx
while i ! = ID [i]:
ID [i], i = ID [ ID [i]], ID [i]
return i
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
def minChange(n, a, b):
for i in range (N):
ID [i] = i
sz[i] = 1
for i in range (n):
if Root(a[i]) ! = Root(b[i]):
Union(a[i], b[i])
ans = 0
for i in range (n):
if ID [i] = = i:
ans + = (sz[i] - 1 )
return ans
a = [ 2 , 1 , 1 , 3 , 5 ]
b = [ 1 , 2 , 2 , 4 , 5 ]
n = len (a)
print (minChange(n, a, b))
|
C#
using System;
class GFG{
static int N=100010;
static int []id= new int [100010];
static int []sz= new int [100010];
static int Root( int idx)
{
int i = idx;
while (i != id[i])
{
id[i] = id[id[i]];
i = id[i];
}
return i;
}
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;
}
}
}
static int minChange( int n, int []a, int []b)
{
for ( int i = 0; i < N; i++)
{
id[i] = i;
sz[i] = 1;
}
for ( int i = 0; i < n; ++i)
if (Root(a[i]) != Root(b[i]))
Union(a[i], b[i]);
int ans = 0;
for ( int i = 0; i < n; ++i)
if (id[i] == i)
ans += (sz[i] - 1);
return ans;
}
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));
}
}
|
PHP
<?php
$N = 100010;
$id = array_fill (0, $N , NULL);
$sz = array_fill (0, $N , NULL);
function Root( $idx )
{
global $id ;
$i = $idx ;
while ( $i != $id [ $i ])
{
$id [ $i ] = $id [ $id [ $i ]];
$i = $id [ $i ];
}
return $i ;
}
function Union( $a , $b )
{
global $sz , $id ;
$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 minChange( $n , & $a , & $b )
{
global $id , $sz , $N ;
for ( $i = 0; $i < $N ; $i ++)
{
$id [ $i ] = $i ;
$sz [ $i ] = 1;
}
for ( $i = 0; $i < $n ; ++ $i )
if (Root( $a [ $i ]) != Root( $b [ $i ]))
Union( $a [ $i ], $b [ $i ]);
$ans = 0;
for ( $i = 0; $i < $n ; ++ $i )
if ( $id [ $i ] == $i )
$ans += ( $sz [ $i ] - 1);
return $ans ;
}
$a = array (2, 1, 1, 3, 5);
$b = array (1, 2, 2, 4, 5);
$n = sizeof( $a );
echo minChange( $n , $a , $b );
?>
|
Javascript
<script>
let N=100010;
let id= new Array(100010);
let sz= new Array(100010);
function Root(idx)
{
let i = idx;
while (i != id[i])
{
id[i] = id[id[i]];
i = id[i];
}
return i;
}
function Union(a,b)
{
let i = Root(a);
let 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 minChange(n,a,b)
{
for (let i = 0; i < N; i++)
{
id[i] = i;
sz[i] = 1;
}
for (let i = 0; i < n; ++i)
if (Root(a[i]) != Root(b[i]))
Union(a[i], b[i]);
let ans = 0;
for (let i = 0; i < n; ++i)
if (id[i] == i)
ans += (sz[i] - 1);
return ans;
}
let a=[2, 1, 1, 3, 5 ];
let b=[ 1, 2, 2, 4, 5 ];
let n = a.length;
document.write(minChange(n, a, b));
</script>
|
Time Complexity: O(N + n) where N is the maximum possible value of an array item and n is the number of elements in the array.
Last Updated :
02 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...