• + 0 comments

    Can some one help me? All tests are failing with me but i think i'm making everything allright. Here is my code

    let test = Int(readLine()!)!
    
    extern: for i in 1...test {
        let itemCount = Int(readLine()!)!
        var items = readLine()!.characters.split(" ").map{Int(String($0))!}
        var swaps = 0
        
        for j in 0 ..< items.count {
            let myPos = items[j]
            var dif = myPos - (j + 1)
            
            if dif < 0 {
                dif *= -1
            }
            
            if (dif > 2) {
                print("Too chaotic")
                continue extern
            }
        }
    
        var it = 0 // 0 = pos 1
        swapsLoop: while it < itemCount {
            for k in 0 ..< itemCount {
                if it + 1 == items[it] {
                    it += 1
                    continue swapsLoop
                }
    
                if items[k] == it + 1 {
                    var aux = items[k]
                    items[k] = items[k - 1]
                    items[k - 1] = aux
                    swaps += 1
                }
            }
        }
        print(swaps)
    }
    

    I bought one test case that is this:

    2
    8
    5 1 2 3 7 8 6 4
    8
    1 2 5 3 7 8 6 4
    

    And wait this response:

    Too chaotic
    7
    

    But the number 4 is too away of his place(both cases). He is 4 steps. It's more than 2, so it prints Too chaotic and i got the wrong anser. Can someone help me?