Skip to main content
Tweeted twitter.com/StackCodeReview/status/1609972812994777090
added 22 characters in body
Source Link
koen
  • 179
  • 8

I am trying to find a way to search an Int array (all numbers are > 0) and identify sequences where the sum of each element is a specific number. One element can be in more than one sequence.

This works:

let input = [2, 52, 23, 52, 28, 47, 13, 15]
let target = 75

var result: [[Int]] = []

let count = input.count
var start = 0

while start < count {
    for index in start..<count {
        let temp = Array(input[start...index])
        let sum = temp.reduce(0, +)
        
        if sum == target {
            result.append(temp)
            break
        }
        
        if sum > target {
            break
        }
    }
    
    start += 1
}


print(result) // [[52, 23], [23, 52], [28, 47], [47, 13, 15]]

However, when the array gets longer and the target higher, it obviously slows down. For instance an array of 10000 elements and a target of 400 takes about 2 seconds. The above example takes 0.008 seconds.

Is there anyway I can improve on my code so that it doesn't slow down so much for larger arrays and targets?

I am trying to find a way to search an Int array and identify sequences where the sum of each element is a specific number. One element can be in more than one sequence.

This works:

let input = [2, 52, 23, 52, 28, 47, 13, 15]
let target = 75

var result: [[Int]] = []

let count = input.count
var start = 0

while start < count {
    for index in start..<count {
        let temp = Array(input[start...index])
        let sum = temp.reduce(0, +)
        
        if sum == target {
            result.append(temp)
            break
        }
        
        if sum > target {
            break
        }
    }
    
    start += 1
}


print(result) // [[52, 23], [23, 52], [28, 47], [47, 13, 15]]

However, when the array gets longer and the target higher, it obviously slows down. For instance an array of 10000 elements and a target of 400 takes about 2 seconds. The above example takes 0.008 seconds.

Is there anyway I can improve on my code so that it doesn't slow down so much for larger arrays and targets?

I am trying to find a way to search an Int array (all numbers are > 0) and identify sequences where the sum of each element is a specific number. One element can be in more than one sequence.

This works:

let input = [2, 52, 23, 52, 28, 47, 13, 15]
let target = 75

var result: [[Int]] = []

let count = input.count
var start = 0

while start < count {
    for index in start..<count {
        let temp = Array(input[start...index])
        let sum = temp.reduce(0, +)
        
        if sum == target {
            result.append(temp)
            break
        }
        
        if sum > target {
            break
        }
    }
    
    start += 1
}


print(result) // [[52, 23], [23, 52], [28, 47], [47, 13, 15]]

However, when the array gets longer and the target higher, it obviously slows down. For instance an array of 10000 elements and a target of 400 takes about 2 seconds. The above example takes 0.008 seconds.

Is there anyway I can improve on my code so that it doesn't slow down so much for larger arrays and targets?

Source Link
koen
  • 179
  • 8

Swift - efficient array searching

I am trying to find a way to search an Int array and identify sequences where the sum of each element is a specific number. One element can be in more than one sequence.

This works:

let input = [2, 52, 23, 52, 28, 47, 13, 15]
let target = 75

var result: [[Int]] = []

let count = input.count
var start = 0

while start < count {
    for index in start..<count {
        let temp = Array(input[start...index])
        let sum = temp.reduce(0, +)
        
        if sum == target {
            result.append(temp)
            break
        }
        
        if sum > target {
            break
        }
    }
    
    start += 1
}


print(result) // [[52, 23], [23, 52], [28, 47], [47, 13, 15]]

However, when the array gets longer and the target higher, it obviously slows down. For instance an array of 10000 elements and a target of 400 takes about 2 seconds. The above example takes 0.008 seconds.

Is there anyway I can improve on my code so that it doesn't slow down so much for larger arrays and targets?