We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- New Year Chaos
- Discussions
New Year Chaos
New Year Chaos
Sort by
recency
|
73 Discussions
|
Please Login in order to post a comment
It’s important to note that during processing, instead of letting the wrong person move back, the focus should be on bringing the correct person forward. After each step of the iteration, the person in the current scanning position should be sorted.
For example, given [2 3 1 4 5]: If '2' is moved back, the result will be: [3 2 1 5 4]
This accidentally allows '3' to gain an additional bribe. Been struggling with this for a while, hope this helps.
For anyone who is struggling but doesn't want the full solution. The question is essentially a reverse bubble sort, just make sure the Too chaotic condition is found first.
pyhton 3
Javascript
It's essentially a reverse insertion sort. You just keep looping through the array doing a single swap until it's sorted. Count how many times you had to make a swap to get it sorted. Break if the value of any element is more than two higher than its original position.
Here is HackerRank New year chaos problem solution in Python, java, C++, C and javascript