Skip to content
Advertisement

Maximum Subarray (Kadane’s algorithm approach)

https://leetcode.com/problems/maximum-subarray/description/

Input test case:

  • [-2,1,-3,4,-1,2,1,-5,4]
  • [-2, -1]
  • [-2, 1]
  • [1]
  • [1, 2]

JavaScript

I wanted to pass this case Input: [-2, -1] so that I modified var currentMax = 0; and var max = 0; to current code.

Apparently, Kadane’s algorithm is needed to include at least one positive number so that second case might not be solved by this.

Is it possible to solve all these test cases by using Kadane’s algorithm or do I need to implement in other ways?

Thanks!

Advertisement

Answer

JavaScript
Advertisement