Thursday, 6 May 2021

Find max num in an array

 def maximumSubarray(nums):

  localMax = globalMax = nums[0]
  for ind, num in enumerate(nums[1:]):
    localMax = max(num, num + localMax)
    globalMax = max(localMax, globalMax)

  return globalMax


nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
print('Maximum sum subarray ->' ,maximumSubarray(nums))
# OUTPUT: Maximum sum subarray -> 6