
import math
import os
import random
import re
import sys

# Complete the minimumBribes function below.
def minimumBribes(finalLine):
  if invalid(finalLine):
    return "Too chaotic"
  return bubbleSort(finalLine)

def invalid(finalLine):
    return any(didBribeMoreThanTwoPeople(person, index) for index, person in enumerate(finalLine))

def didBribeMoreThanTwoPeople(person, index):
    return index + 2 < person - 1

def bubbleSort(line):
    swaps = 0
    numberOfPeople = len(line)

    for person in range(numberOfPeople):
        for i in range(0, numberOfPeople - person - 1):
            if line[i] > line[i + 1]:
                line[i], line[i + 1] = line[i + 1], line[i]
                swaps += 1

    return swaps


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

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

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

        print(minimumBribes(q))


import math
import os
import random
import re
import sys

# Complete the minimumBribes function below.
def minimumBribes(finalLine):
  if invalid(finalLine):
    return "Too chaotic"
  return bubbleSort(finalLine)

def invalid(finalLine):
    return any(didBribeMoreThanTwoPeople(person, index) for index, person in enumerate(finalLine))

def didBribeMoreThanTwoPeople(person, index):
    return index + 2 < person - 1

def bubbleSort(line):
    swaps = 0
    swappedInCurrentPass = False

    for person in range(len(line)):
        for i in range(0, len(line) - 1):
            if line[i] > line[i + 1]:
                line[i], line[i + 1] = line[i + 1], line[i]
                swaps += 1
                swappedInCurrentPass = True
        if swappedInCurrentPass:
            swappedInCurrentPass = False
        else:
            break

    return swaps


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

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

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

        print(minimumBribes(q))

Improve efficiency of new year chaos
Improve efficiency of new year chaos

  
  import math
  import os
  import random
  import re
  import sys
  
  # Complete the minimumBribes function below.
  def minimumBribes(finalLine):
    if invalid(finalLine):
      return "Too chaotic"
    return bubbleSort(finalLine)
  
  def invalid(finalLine):
      return any(didBribeMoreThanTwoPeople(person, index) for index, person in enumerate(finalLine))
  
  def didBribeMoreThanTwoPeople(person, index):
      return index + 2 < person - 1
  
  def bubbleSort(line):
      swaps = 0
-     numberOfPeople = len(line)
+     swappedInCurrentPass = False
  
-     for person in range(numberOfPeople):
-         for i in range(0, numberOfPeople - person - 1):
+     for person in range(len(line)):
+         for i in range(0, len(line) - 1):
              if line[i] > line[i + 1]:
                  line[i], line[i + 1] = line[i + 1], line[i]
                  swaps += 1
+                 swappedInCurrentPass = True
+         if swappedInCurrentPass:
+             swappedInCurrentPass = False
+         else:
+             break
  
      return swaps
  
  
  if __name__ == '__main__':
      t = int(input())
  
      for t_itr in range(t):
          n = int(input())
  
          q = list(map(int, input().rstrip().split()))
  
          print(minimumBribes(q))
  
Improve efficiency of new year chaos
## Code Before:

import math
import os
import random
import re
import sys

# Complete the minimumBribes function below.
def minimumBribes(finalLine):
  if invalid(finalLine):
    return "Too chaotic"
  return bubbleSort(finalLine)

def invalid(finalLine):
    return any(didBribeMoreThanTwoPeople(person, index) for index, person in enumerate(finalLine))

def didBribeMoreThanTwoPeople(person, index):
    return index + 2 < person - 1

def bubbleSort(line):
    swaps = 0
    numberOfPeople = len(line)

    for person in range(numberOfPeople):
        for i in range(0, numberOfPeople - person - 1):
            if line[i] > line[i + 1]:
                line[i], line[i + 1] = line[i + 1], line[i]
                swaps += 1

    return swaps


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

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

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

        print(minimumBribes(q))

## Instruction:
Improve efficiency of new year chaos
## Code After:

import math
import os
import random
import re
import sys

# Complete the minimumBribes function below.
def minimumBribes(finalLine):
  if invalid(finalLine):
    return "Too chaotic"
  return bubbleSort(finalLine)

def invalid(finalLine):
    return any(didBribeMoreThanTwoPeople(person, index) for index, person in enumerate(finalLine))

def didBribeMoreThanTwoPeople(person, index):
    return index + 2 < person - 1

def bubbleSort(line):
    swaps = 0
    swappedInCurrentPass = False

    for person in range(len(line)):
        for i in range(0, len(line) - 1):
            if line[i] > line[i + 1]:
                line[i], line[i + 1] = line[i + 1], line[i]
                swaps += 1
                swappedInCurrentPass = True
        if swappedInCurrentPass:
            swappedInCurrentPass = False
        else:
            break

    return swaps


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

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

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

        print(minimumBribes(q))
