It is New Year’s Day and people are in line for the Wonderland rollercoaster ride. Each person wears a sticker indicating their initial position in the queue from 1 to n. Any person can bribe the person directly in front of them to swap positions, but they still wear their original sticker. One person can bribe at most two others.
Determine the minimum number of bribes that took place to get to a given queue order. Print the number of bribes, or, if anyone has bribed more than two people, print Too chaotic.
Go to problem statement

Explanation Video:

Mentor: Satyendra Jaiswal
Langauge: Python

Source Code:  for `minimumBribes` function

``````
# Complete the minimumBribes function below.
def minimumBribes(q):
c=0
for i in range(len(q)-1,0,-1):
if(q[i]!=i+1):
if(q[i-1]==i+1):
c+=1
q[i],q[i-1]=q[i-1],q[i]
#    print('swap1----->',q)
elif(q[i-2]==i+1):
c+=2
q[i-2],q[i-1]=q[i-1],q[i-2]
q[i-1],q[i]=q[i],q[i-1]
#           print('swap2------->',q)
else:
print('Too chaotic')
return
print(c)

``````
coming soon…
coming soon…

Full Source Code: New Year Chaos | HackerRank

``````
#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the minimumBribes function below.
def minimumBribes(q):
c=0
for i in range(len(q)-1,0,-1):
if(q[i]!=i+1):
if(q[i-1]==i+1):
c+=1
q[i],q[i-1]=q[i-1],q[i]
#    print('swap1----->',q)
elif(q[i-2]==i+1):
c+=2
q[i-2],q[i-1]=q[i-1],q[i-2]
q[i-1],q[i]=q[i],q[i-1]
#           print('swap2------->',q)
else:
print('Too chaotic')
return
print(c)

if __name__ == '__main__':
t = int(input())

for t_itr in range(t):
n = int(input())

q = list(map(int, input().rstrip().split()))

minimumBribes(q)

``````
coming soon…
coming soon…