New Year Chaos

  • + 2 comments

    A difficult one to me. It took me a lot of time.

    First: number cannot move more than 2 places forward. Second: Calculating difference between original positions and current posistions is not enough. (Sample test 1) Third: For every element you need to check, how many bigger elements are in front of them and that is enough. Third*: But simply iterating through the list of 'already reviewed' items is too slow. Fourth: Combining points First and Third you can come up with a correct answer.

    I will paste incorrect code that is too slow to finish the task. That is what I meant by point Third*.

    n = 0
    looked = []
    for i, el in enumerate(q, 1):
        if el - i > 2:
            print('Too chaotic')
            return
        looked.append(el)
        n+= sum([1 for e in looked if e > el])
    print(n)