{"id":425,"date":"2020-03-26T05:38:00","date_gmt":"2020-03-26T05:38:00","guid":{"rendered":"https:\/\/danwritescode.com\/?p=425"},"modified":"2020-06-26T06:15:40","modified_gmt":"2020-06-26T06:15:40","slug":"max-slice-sum-codility-100-correct-javascript-solution","status":"publish","type":"post","link":"https:\/\/danwritescode.com\/max-slice-sum-codility-100-correct-javascript-solution\/","title":{"rendered":"Max Slice Sum – Codility 100% Correct Javascript Solution"},"content":{"rendered":"\n

The Max Slice Sum was a fun brain challenge. <\/p>\n\n\n\n

I ended up finding the O(N) complexity solution after using the ol’ pen and paper trials.<\/p>\n\n\n\n

So, I iterated over the array. At each step I calculated the maximum sum ending at the current element by comparing the minimum sum allowed, the current element, and the maximum previous ending + the current element.
The max slice is the maximum between the previous max slice and the maximum ending at the current place. <\/p>\n\n\n\n

Solution in Javascript with O(N) complexity.<\/p>\n\n\n\n

function solution(A) {\n    let minSum =  0-2147483648;\n    let maxSlice = minSum\n    let maxEnding = 0; \n    for (let i=0; i<A.length; i++){\n\n        maxEnding = Math.max(minSum, maxEnding+A[i], A[i])\n\n        maxSlice = Math.max(maxEnding, maxSlice)\n\n    }   \n    if (maxSlice > 2147483647) {\n        maxSlice = 2147483647\n    }\n    return maxSlice\n    \n}<\/code><\/pre>\n\n\n\n

And here is the description and the algo report<\/a> on Codility.<\/p>\n","protected":false},"excerpt":{"rendered":"

The Max Slice Sum was a fun brain challenge. I ended up finding the O(N) complexity solution after using the ol’ pen and paper trials. So, I iterated over the array. At each step I calculated the maximum sum ending at the current element by comparing the minimum sum allowed, the current element, and the maximum previous ending + the current element.The max slice is the maximum between the previous max slice and the maximum ending at the current place. Solution in Javascript with O(N) complexity. And here is the description and the algo report on Codility.<\/p>\n","protected":false},"author":1,"featured_media":426,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,12,6],"tags":[],"_links":{"self":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts\/425"}],"collection":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/comments?post=425"}],"version-history":[{"count":0,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts\/425\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/media\/426"}],"wp:attachment":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/media?parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/categories?post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/tags?post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}