Young Energy Is Not Enough
Time limit1sMemory limit256 MB
Partition 3N ages into N triples; each triple's energy is its median age. Minimize the spread between the largest and smallest median.
- Level
Medium6 of 10
- Topics
- Sorting, Greedy, Binary search, Array
- Solved
- No attempts yet
Problem
The reality survival show is looking for the best street algorithm crew in South Korea. college students who are said to be good at algorithms from all over the country have joined, and they will form crews of 3 to compete in street algorithm battles.
But computer science students have always coded alone, so the production team has to make the crews...
The production team is worried that a crew might lack young energy. So the production team will form the crews with the following in mind.
- Call the median age of a crew's members, that is, the age of the second oldest of the three members, the crew's energy.
- The production team must minimize the difference in energy between the crew with the highest energy and the crew with the lowest energy.
Find this minimized value.
Input
The first line gives . There are participants in total.
The second line gives integers separated by spaces. The -th integer is the age of the -th participant.
Output
Form the crews from the participants appropriately, and output the energy difference when the difference in energy between the crew with the highest energy and the crew with the lowest energy is minimized.