Minimum changes required to make two arrays identical
Last Updated :
02 Sep, 2022
Given two arrays, A and B with n elements each. The task is to make these two arrays identical i:e, for each 1\leq i \leq N , we want to make A_{i} = B_{i} . 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 1\leq i \leq N . 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 ans = \sum_{i=1}^{N} (sz[i]-1) . We subtract 1 because, initially, we take the size of each set to be 1.
Implementation:
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
Python3
# 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
# required 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 different 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
<?php
// PHP 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 = array_fill(0, $N, NULL);
$sz = array_fill(0, $N, NULL);
// Function to assign root
function Root($idx)
{
global $id;
$i = $idx;
while ($i != $id[$i])
{
$id[$i] = $id[$id[$i]];
$i = $id[$i];
}
return $i;
}
// Function to find Union
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 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
?>
JavaScript
<script>
// Javascript program to find minimum changes
// required to make two arrays identical
let N=100010;
/* 'id': stores parent of a node.
'sz': stores size of a DSU tree. */
let id=new Array(100010);
let sz=new Array(100010);
// Function to assign root
function Root(idx)
{
let i = idx;
while (i != id[i])
{
id[i] = id[id[i]];
i = id[i];
}
return i;
}
// Function to find Union
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 to find minimum changes required
// to make both array equal.
function minChange(n,a,b)
{
// Sets as single elements
for (let i = 0; i < N; i++)
{
id[i] = i;
sz[i] = 1;
}
// Combine items if they belong to different
// sets.
for (let 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.
let ans = 0;
for (let i = 0; i < n; ++i)
if (id[i] == i)
ans += (sz[i] - 1);
return ans;
}
// Driver program
let a=[2, 1, 1, 3, 5 ];
let b=[ 1, 2, 2, 4, 5 ];
let n = a.length;
document.write(minChange(n, a, b));
// This code is contributed by rag2127
</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.
Similar Reads
Minimum changes required to make all element in an array equal Given an array of length N, the task is to find minimum operation required to make all elements in the array equal. Operation is as follows: Replace the value of one element of the array by one of its adjacent elements. Examples: Input: N = 4, arr[] = {2, 3, 3, 4} Output: 2 Explanation: Replace 2 an
5 min read
Minimum operations required to make two elements equal in Array Given array A[] of size N and integer X, the task is to find the minimum number of operations to make any two elements equal in the array. In one operation choose any element A[i] and replace it with A[i] & X. where & is bitwise AND. If such operations do not exist print -1. Examples: Input:
9 min read
Minimum swaps to make two arrays consisting unique elements identical Given two arrays a[] and b[] of the same length, containing the same values but in different orders (with no duplicates).The task is to make b[] identical to a[] using the minimum number of swaps.Examples: Input: a[] = [3, 6, 4, 8], b[] = [4, 6, 8, 3]Output: 2Explanation: To make b[] identical to a[
10 min read
Minimum operations required to make all the array elements equal Given an array arr[] of n integer and an integer k. The task is to count the minimum number of times the given operation is required to make all the array elements equal. In a single operation, the kth element of the array is appended at the end of the array and the first element of the array gets d
6 min read
Minimize the shift required to make arr1[i] <= arr2[i] Given two sorted non-decreasing arrays arr1[] and arr2[] of length n, the objective is to modify arr1 such that for every index i, arr1[i] <= arr2[i].If arr1[i] is greater than arr2[i], then all the elements to the right of arr1[i] (including arr1[i]) need to be shifted one position to the right,
5 min read