Left Rotation

  • + 0 comments

    Swift solution:

    func rotateLeft(d: Int, arr: [Int]) -> [Int] {
        let n = arr.count
        
        // 'mod' operation avoids unnecessary rotations
        let rotations = d % n
        
        return Array(arr[rotations...] + arr[..<rotations])
    }