educative.io

Could you update the non-negative number solution

I write the code using Swift, but remaining count is different. So What’s the best solution for checking it?

func makeSquaresStartFromZero(input: [Int]) -> [Int] {
    var output: [Int] = Array(repeating: 0, count: input.count)
    let startPoint = input.distance(from: input[0], to: 0)
    
    var leftIndex = startPoint - 1
    var rightIndex = startPoint
    var lowestIndex = 0
    
    while leftIndex >= 0 && rightIndex < input.count {
        let leftItem = input[leftIndex] * input[leftIndex]
        let rightItem = input[rightIndex] * input[rightIndex]
        if leftItem > rightItem {
            output[lowestIndex] = rightItem
            rightIndex += 1
        }
        else {
            output[lowestIndex] = leftItem
            leftIndex -= 1
        }
        lowestIndex += 1
    }
    if rightIndex < input.count {
        for i in stride(from: rightIndex, to: input.count, by: 1) {
            output[lowestIndex] = input[i] * input[i]
            lowestIndex += 1
        }
    }
    else {
        for i in stride(from: leftIndex, through: 0, by: -1) {
            output[lowestIndex] = input[i] * input[i]
            lowestIndex += 1
        }
    }
    return output
}