N
N
Nik_Haker2015-10-04 20:41:01
C++ / C#
Nik_Haker, 2015-10-04 20:41:01

How to find the minimum number of permutations?

Let's call uniform a set of natural numbers in which even and odd numbers alternate: even-odd-even-odd... or odd-even-odd... Given a set of 2N natural numbers, of which exactly N are even and N are odd . It is necessary to determine for what MINIMUM number of permutations it can be made uniform. By permutation we consider the exchange of places of two e-comrade. Are there any formulas for this?
I need to write a program to calculate. I will write the program myself, the question is how to calculate the minimum number of permutations? even for a person, manually, it is difficult to say what is the minimum .. are there any formulas for this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2015-10-04
@Mercury13

Consider the order even-odd-even-odd...
We count how many even ones are not in their places (=a).
And how many odd ones are out of place (=b).
If a≠b, we do not have N even and N odd - we output "invalid data set".
And if they are equal, then a substitutions are needed for the even-odd order. For the opposite - Na.
So the answer is min(a, N−a).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question