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.
Bribe/Swap 4 to its position
1 2 3 5 6 7 8 4: [(4,5), (4,6), (4,7), (4,8)]
Swap 6 to its position
1 2 3 5 7 8 6 4: [(6, 7), (6, 8)]
Swap 3 and 5
1 2 5 3 7 8 6 4 [(3, 5)]
I suspect that you were trying to do something like bribe=sum(abs(q[i] - 1 - i)). Notice that when we swap 6 to its position we increased bribe by 1 even though 2 swaps were required.
Here's a simpler test case: 2 4 3 1
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
New Year Chaos
You are viewing a single comment's thread. Return to all comments →
Reply to https://www.hackerrank.com/challenges/one-month-preparation-kit-new-year-chaos/forum/comments/1083532
1 2 3 4 5 6 7 8
Bribe/Swap 4 to its position 1 2 3 5 6 7 8 4: [(4,5), (4,6), (4,7), (4,8)]
Swap 6 to its position 1 2 3 5 7 8 6 4: [(6, 7), (6, 8)]
Swap 3 and 5 1 2 5 3 7 8 6 4 [(3, 5)]
I suspect that you were trying to do something like
bribe=sum(abs(q[i] - 1 - i))
. Notice that when we swap 6 to its position we increasedbribe
by 1 even though 2 swaps were required.Here's a simpler test case: 2 4 3 1