Given two arrays A[] and B[] of same size n. We need to first permute any of arrays such that the sum of product of pairs( 1 element from each) is minimum. That is SUM ( Ai*Bi) for all i is minimum. We also need to count number of de-arrangements present in original array as compared to permuted array.
Examples:
Input : A[] = {4, 3, 2},
B[] = {7, 12, 5}
Output : 3
Explanation : A[] = {4, 3, 2} and B[] = {5, 7, 12}
results in minimum product sum. B[] = {7, 12, 5}
is 3-position different from new B[].
Input : A[] = {4, 3, 2},
B[] = { 1, 2, 3}
Output : 0
Explanation : A[] = {4, 3, 2} and B[] = {1, 2, 3}
results in minimum product sum. B[] = {1, 2, 3}
is exactly same as new one.
Idea behind finding the minimum sum of product from two array is to sort both array one in increasing and other in decreasing manner. These type of arrays will always produce minimum sum of pair product. Sorting both array will give the pair value i.e. which element from A is paired to which element from B[]. After that count the de-arrangement from original arrays.
Algorithm :
- make a copy of both array.
- sort copy_A[] in increasing, copy_B[] in decreasing order.
- Iterate for all Ai, find Ai in copy_A[] as copy_A[j] and check whether copy_B[j] == B[i] or not. Increment count if not equal.
- Return Count Value. That will be our answer.
Implementation:
CPP
#include<bits/stdc++.h>
using namespace std;
int findDearrange ( int A[], int B[], int n)
{
vector < int > copy_A (A, A+n);
vector < int > copy_B (B, B+n);
sort(copy_A.begin(), copy_A.end());
sort(copy_B.begin(), copy_B.end(),greater< int >());
int count = 0;
for ( int i=0; i<n;i++)
{
vector< int >::iterator itA;
itA = lower_bound(copy_A.begin(),
copy_A.end(), A[i]);
if (B[i] != copy_B[itA-copy_A.begin()])
count++;
}
return count;
}
int main()
{
int A[] = {1, 2, 3, 4};
int B[] = {6, 3, 4, 5};
int n = sizeof (A) / sizeof (A[0]);;
cout << findDearrange(A,B,n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static Integer findDearrange (Integer A[], Integer B[], Integer n)
{
Integer copy_A[]=A.clone();
Integer copy_B[]=B.clone();
Arrays.sort(copy_A);
Arrays.sort(copy_B, Collections.reverseOrder());
Integer count = 0 ;
for (Integer i= 0 ; i<n;i++)
{
Integer itA;
itA = Arrays.binarySearch(copy_A, A[i]);
if (B[i] != copy_B[itA])
count++;
}
return count;
}
public static void main (String[] args)
{
Integer A[] = { 1 , 2 , 3 , 4 };
Integer B[] = { 6 , 3 , 4 , 5 };
Integer n = A.length;
System.out.println(findDearrange(A,B,n));
}
}
|
Python3
import copy
def findDearrange(A, B, n):
copy_A = copy.deepcopy(A)
copy_B = copy.deepcopy(B)
copy_A.sort()
copy_B.sort(reverse = True )
count = 0
for i in range (n):
itA = None
itA = copy_A.index(A[i])
if B[i] ! = copy_B[itA]:
count + = 1
return count
A = [ 1 , 2 , 3 , 4 ]
B = [ 6 , 3 , 4 , 5 ]
n = len (A)
print (findDearrange(A, B, n))
|
C#
using System;
class GFG
{
static int FindDearrange( int [] A, int [] B, int n)
{
int [] copy_A = ( int [])A.Clone();
int [] copy_B = ( int [])B.Clone();
Array.Sort(copy_A);
Array.Sort(copy_B, new Comparison< int >((a, b) => b.CompareTo(a)));
int count = 0;
for ( int i = 0; i < n; i++)
{
int itA = Array.BinarySearch(copy_A, A[i]);
if (B[i] != copy_B[itA])
count++;
}
return count;
}
static void Main( string [] args)
{
int [] A = { 1, 2, 3, 4 };
int [] B = { 6, 3, 4, 5 };
int n = A.Length;
Console.WriteLine(FindDearrange(A, B, n));
}
}
|
Javascript
function findDearrange(A, B, n) {
const copy_A = [...A];
const copy_B = [...B];
copy_A.sort((a, b) => a - b);
copy_B.sort((a, b) => b - a);
let count = 0;
for (let i = 0; i < n; i++) {
let itA = null ;
itA = copy_A.indexOf(A[i]);
if (B[i] !== copy_B[itA]) {
count += 1;
}
}
return count;
}
const A = [1, 2, 3, 4];
const B = [6, 3, 4, 5];
const n = A.length;
console.log(findDearrange(A, B, n));
|
Time Complexity: O(n logn).
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Feb, 2023
Like Article
Save Article