Problem :

https://www.hackerrank.com/challenges/new-year-chaos/problem


My Solution :

#!/usr/bin/env python3

def minimumBribes(q):
    i = -1
    memo = {}
    while i < len(q)-2:
        i += 1
        i = max(0, i)
        if q[i] == i+1:
            continue
        if q[i] > q[i+1]:
            if memo.setdefault(q[i], 0) == 2:
                return 'Too chaotic'
            memo[q[i]] += 1
            q[i], q[i+1] = q[i+1], q[i]
            i -= 2
    return sum(memo.values())


t = int(input())
for t_itr in range(t):
    n = int(input())
    q = list(map(int, input().rstrip().split()))
    result = minimumBribes(q)
    print(result)