Rearrange 2nd sequence so that no two elements at same index are equal.

45 Views Asked by At

Given 2 sequences a and b what is best way to rearrange the b array so that no two elements at same index are equal.

Example
a = 1 1 1 2 2 3
b = 1 1 1 2 2 3

we can rearrange b to make 2 2 3 1 1 1 and hence no two elements at same index are same.

Note

It can be possible that there is no rearrangement as well.
Assume 1st sequence is sorted

1

There are 1 best solutions below

0
On

This is the matching (or marriage) problem, where „boy“ $i$ and „girl“ $j$ can match iff $a_i\ne b_j$.