#K12341. Largest Sum Contiguous Subarray

    ID: 23669 Type: Default 1000ms 256MiB

Largest Sum Contiguous Subarray

Largest Sum Contiguous Subarray

Given an array of integers, find the contiguous subarray which has the largest sum and output that sum.

Your task is to implement an algorithm with a time complexity of \(O(n)\) (using Kadane's algorithm) to efficiently solve the problem. The array may contain both positive and negative numbers. If the array is empty, your program should output an error message.

For example, for the input array [1, 2, 3, 4, 5], the output should be 15, and for the input array [-2, 1, -3, 4, -1, 2, 1, -5, 4], the output should be 6.

inputFormat

The first line of input contains a single integer (n) (number of elements in the array). The second line contains (n) space-separated integers representing the elements of the array. If (n = 0), the array is considered empty.

outputFormat

Output a single line containing the largest sum of a contiguous subarray. If the array is empty, output an error message, for example, "Error".## sample

5
1 2 3 4 5
15

</p>