[Python] In a one dimensional numeric array, can we get largest sum value. however can we get...

Discussão em 'Python' iniciado por Stack, Julho 3, 2025.

  1. Stack

    Stack Membro Participativo

    I have a piece of code and want them to find max sum for float and complex number too. however i am getting error

    def max_subarray_sum(nums: list) -> complex:
    if not nums:
    return 0
    max_sum = current_sum = nums[0]
    for num in nums[1:]:
    current_sum = max(num, current_sum + num)
    max_sum = max(max_sum, current_sum)
    return max_sum

    print(max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4])) # 6
    print(max_subarray_sum([1])) # 1
    print(max_subarray_sum([5, 4, -1, 7, 8])) # 23
    print(max_subarray_sum([-1, -2, -3, -4])) # -1 (the largest single element)
    print(max_subarray_sum([1, 2, 3, 4, 5])) # 15 (the entire array)`


    I tried to create a array with float and complex number however its value did not take up.

    what are the other way to find sum max and min in one dimensional array.

    Continue reading...

Compartilhe esta Página