Harry Potter and the Battle for Hogwarts
InterviewTime limit2sMemory limit1024 MB
Apply k passes of bubble sort to a permutation of 1 to n and print the array after those passes.
- Level
Medium6 of 10
- Topics
- Array, Implementation
- Solved
- No attempts yet
Passes
The decisive battle for Hogwarts is coming. All of its inhabitants have gathered in the hall to repel the enemy's attack. Each defender of Hogwarts has a magic power, which is a natural number from to . All defenders have different magic powers.
Harry decided that the line of defenders would best repel the attack if the magic power increases from left to right. However, the defenders are already standing in a line that may not satisfy this condition.
To repel the attack, Harry wants to rearrange the defenders in increasing order of magic power. He uses the following algorithm. He walks from left to right and looks at each pair of neighboring defenders. If the left defender is stronger than the right one, he swaps them.
From the course "Fundamentals of Muggle Informatics", Harry knows that after such passes, all defenders will be in increasing order of magic power. However, time is short, so Harry can only make passes.
Help Harry find the order in which the defenders will meet the enemy army.
Input
The first line contains an integer (), the number of defenders, and an integer (), the number of passes Harry can make. The second line contains integers (), the magic powers of the defenders in the original line from left to right.
Output
Print integers on a single line, the magic powers of the defenders from left to right after Harry makes passes.