top of page

Looking to master object-oriented and system design for tech interviews or career growth?

  • Improve your system design and machine coding skills.

  • Study with our helpful resources.

  • Prepare for technical interviews and advance your career.

**We're in beta mode and would love to hear your feedback.

Max Sum Contiguous Subarray - Interviewbit Solution

Sep 1, 2020
1 min read

Updated: Sep 8, 2020


Problem Description:

Find the contiguous subarray within an array, A of length N which has the largest sum.


Input Format:

The first and the only argument contains an integer array, A.

Output Format:

Return an integer representing the maximum possible sum of the contiguous subarray. 

Constraints:

1 <= N <= 1e6
-1000 <= A[i] <= 1000 

For example:

Input 1:           A = [1, 2, 3, 4, -10]  
Output 1:          10  
Explanation 1:     The subarray [1, 2, 3, 4] has the maximum possible     
                   sum of 10.  
Input 2:           A = [-2, 1, -3, 4, -1, 2, 1, -5, 4]  
Output 2:          6  
Explanation 2:     The subarray [4,-1,2,1] has the maximum possible sum 
                   of 6. 

Solution:


1 Comment


https://casinoenligneenfrance.net is where I sometimes end up when I'm killing time between coding sessions, and the point about Kadane's algorithm resetting the running sum to zero whenever it drops below zero was the part that finally made the logic click for me. I've always handled this with a brute-force nested loop in interviews and wondered why it felt so slow, so seeing the O(n) version laid out with the running-sum reset is genuinely useful. The second example with the negative prefix really shows why that reset matters.

Like
bottom of page