First of all, this is not a homework or something like that, this is a suggestive problem from my last question Candidate in finding majority element in an array.
There are n
kinds of objects O1, O2, ..., On, and there is an array F[1...n]
, F[i]
is the number of Oi(i.e. there are F[i]
Ois, array F[1...n]
is given), and every F[i]
> 0
.
Now use the following rule to pair objects:
if i != j
, Oi can be paired with Oj,
else if i == j
, Oi can not be paired with Oj.
i.e., only two different kinds of objects can be paired with each other.
Whether or not a valid pairing method may exist for the input array
F[1...n]
? Give out an algorithm with best time complexity to tell true or false and proves its correctness.If there exist, output one valid paring sequence. Give out your algorithm and time/space complexity analysis.
For example,
input F[] = {10, 2, 4, 4};
Then there exists at least one valid pairing method:
2 O1s and 2 O2s paired, 4 O1s and 4 O3s paired, 4 O1s and 4 O4s paired.
One valid pairing sequence is:
(1,2) (1,2) (1,3) (1,3) (1,3) (1,3) (1,4) (1,4) (1,4) (1,4)