Guest User

Untitled

a guest
Dec 15th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. let inputA = [-1,-2,-3,-4]
  2. var resultA = [Int]()
  3. var maxSum = -100
  4.  
  5. for i in 0..<inputA.count{
  6. for j in stride(from: inputA.count - 1, to: -1, by: -1) {
  7. if i <= j { //lower bound should be less than or equal to the higher bound
  8. let subArray = inputA[i...j]
  9. //print(subArray)
  10. let sumOfSubArray = subArray.reduce(0,{$0+$1}) //Logic to calculate sum of subArray Elements
  11. if sumOfSubArray >= maxSum { //base condition
  12. resultA = [] //empty array
  13. maxSum = sumOfSubArray
  14. resultA += subArray
  15. }
  16. }
  17. }
  18. }
  19. print(resultA)
Add Comment
Please, Sign In to add comment