CF BUDDY
← Problems·

1091E · New Year and the Acquaintance Estimation

2400 · binary search, data structures, graphs

Problem: Bob is an active user of the social network Faithbug. On this network, people are able to engage in a mutual friendship. That is, if aa is a friend of bb, then bb is also a friend of aa. Each user thus has a non-negative amount of friends.

This morning, somebody anonymously sent Bob the following link: graph realization problem and Bob wants to know who that was. In order to do that, he first needs to know how the social network looks like. He investigated the profile of every other person on the network and noted down the number of his friends. However, he neglected to note down the number of his friends. Help him find out how many friends he has. Since there may be many possible answers, print all of them.

Input Format: The first line contains one integer nn (1n51051 \leq n \leq 5 \cdot 10^5), the number of people on the network excluding Bob.

The second line contains nn numbers a1,a2,,ana_1,a_2, \dots, a_n (0ain0 \leq a_i \leq n), with aia_i being the number of people that person ii is a friend of.

Output Format: Print all possible values of an+1a_{n+1} — the amount of people that Bob can be friend of, in increasing order.

If no solution exists, output 1-1.

Note: In the first test case, the only solution is that everyone is friends with everyone. That is why Bob should have 33 friends.

In the second test case, there are three possible solutions (apart from symmetries):

  • aa is friend of bb, cc is friend of dd, and Bob has no friends, or
  • aa is a friend of bb and both cc and dd are friends with Bob, or
  • Bob is friends of everyone.

The third case is impossible to solve, as the second person needs to be a friend with everybody, but the first one is a complete stranger.

Sample Cases

Case 1

Input

3
3 3 3

Output

3

Case 2

Input

4
1 1 1 1

Output

0 2 4

Case 3

Input

2
0 2

Output

-1

Case 4

Input

35
21 26 18 4 28 2 15 13 16 25 6 32 11 5 31 17 9 3 24 33 14 27 29 1 20 4 12 7 10 30 34 8 19 23 22

Output

13 15 17 19 21

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback