Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let inputA = [-1,-2,-3,-4]
- var resultA = [Int]()
- var maxSum = -100
- for i in 0..<inputA.count{
- for j in stride(from: inputA.count - 1, to: -1, by: -1) {
- if i <= j { //lower bound should be less than or equal to the higher bound
- let subArray = inputA[i...j]
- //print(subArray)
- let sumOfSubArray = subArray.reduce(0,{$0+$1}) //Logic to calculate sum of subArray Elements
- if sumOfSubArray >= maxSum { //base condition
- resultA = [] //empty array
- maxSum = sumOfSubArray
- resultA += subArray
- }
- }
- }
- }
- print(resultA)
Add Comment
Please, Sign In to add comment