def max_sub_array(nums): # Initialize variables to keep track of the current subarray sum and the maximum subarray sum. current_sum = nums[0] max_sum = nums[0] # Iterate through the array starting from the second element. for i in range(1, len(nums)): # Calculate the current subarray sum by considering two possibilities: # 1. The current element creates a new subarray with a higher sum (nums[i] itself). # 2. The current element extends the subarray ending at i-1 with the current element. current_sum = max(nums[i], current_sum + nums[i]) # Update the maximum subarray sum seen so far. max_sum = max(max_sum, current_sum) # Return the maximum subarray sum. return max_sum # Driver code def main(): inputs = [[1, 2, 2, 3, 3, 1, 4], [2, 2, 1], [4, 1, 2, 1, 2], [-4, -1, -2, -1, -2], [25]] for i in range(len(inputs)): print(i + 1, ".\tArray: ", inputs[i], sep="") print("\tResult: ", max_sub_array(inputs[i]), sep="") print("-" * 100) if __name__ == "__main__": main()