Find the largest sum of any contiguous non-empty subarray.
Input
- Line 1: n (
1 ≤ n ≤ 10^5)
- Line 2: n integers (each between
-10^4 and 10^4)
Output
The maximum subarray sum.
Why interviewers like it
It looks like a brute-force O(n²) problem until you notice you only need the best sum ending here. That jump to O(n) is Kadane's algorithm, and explaining it out loud is the real test.
Read from standard input and write to standard output. 5 test cases in total (3 hidden). Tests for this language run inside your browser.