← 返回博客列表
Google

Google VO Real Question: Find Pair with Maximum Subarray Sum - O(n) Prefix Sum Solution

2026-01-24

Problem Description

Given an integer array, find two non-overlapping subarrays such that the sum of their subarray sums is maximized.

Example:

Input: [1, -2, 3, 4, -5, 8]
Output: 15
Explanation: Subarray [3, 4] (sum=7) and [8] (sum=8), total sum=15

Solution Approaches

Approach 1: Brute Force O(n⁴)

The most straightforward approach is to enumerate all possible pairs of subarrays and calculate their sums.

def maxSumTwoPairs(arr):
    n = len(arr)
    max_sum = float('-inf')
    
    # Enumerate first subarray
    for i in range(n):
        sum1 = 0
        for j in range(i, n):
            sum1 += arr[j]
            
            # Enumerate second subarray (must be after first)
            for k in range(j + 1, n):
                sum2 = 0
                for l in range(k, n):
                    sum2 += arr[l]
                    max_sum = max(max_sum, sum1 + sum2)
    
    return max_sum

Time Complexity: O(n⁴)
Space Complexity: O(1)

Approach 2: Prefix Sum Optimization O(n²)

Using a prefix sum array, we can calculate any subarray sum in O(1) time.

def maxSumTwoPairs(arr):
    n = len(arr)
    
    # Build prefix sum array
    prefix = [0] * (n + 1)
    for i in range(n):
        prefix[i + 1] = prefix[i] + arr[i]
    
    def getSum(i, j):
        # Calculate sum of arr[i:j+1]
        return prefix[j + 1] - prefix[i]
    
    max_sum = float('-inf')
    
    # Enumerate split points
    for split in range(n):
        # First subarray in [0, split]
        # Second subarray in [split+1, n-1]
        
        max_sum1 = float('-inf')
        for i in range(split + 1):
            for j in range(i, split + 1):
                max_sum1 = max(max_sum1, getSum(i, j))
        
        max_sum2 = float('-inf')
        for i in range(split + 1, n):
            for j in range(i, n):
                max_sum2 = max(max_sum2, getSum(i, j))
        
        if max_sum1 != float('-inf') and max_sum2 != float('-inf'):
            max_sum = max(max_sum, max_sum1 + max_sum2)
    
    return max_sum

Time Complexity: O(n²)
Space Complexity: O(n)

Approach 3: Dynamic Programming O(n) ⭐ Optimal

Key observation: For each split point, we need to quickly find the maximum subarray sum on the left and right sides.

Use two DP arrays:

def maxSumTwoPairs(arr):
    n = len(arr)
    if n < 2:
        return 0
    
    # left_max[i] = max subarray sum in arr[0:i+1]
    left_max = [0] * n
    max_ending_here = arr[0]
    left_max[0] = arr[0]
    
    for i in range(1, n):
        max_ending_here = max(arr[i], max_ending_here + arr[i])
        left_max[i] = max(left_max[i - 1], max_ending_here)
    
    # right_max[i] = max subarray sum in arr[i:n]
    right_max = [0] * n
    max_ending_here = arr[n - 1]
    right_max[n - 1] = arr[n - 1]
    
    for i in range(n - 2, -1, -1):
        max_ending_here = max(arr[i], max_ending_here + arr[i])
        right_max[i] = max(right_max[i + 1], max_ending_here)
    
    # Enumerate split points to find maximum sum
    max_sum = float('-inf')
    for i in range(n - 1):
        max_sum = max(max_sum, left_max[i] + right_max[i + 1])
    
    return max_sum

Time Complexity: O(n)
Space Complexity: O(n)

Key Points

  1. Kadane's Algorithm: Find maximum subarray sum in O(n) time
  2. Prefix Sum Concept: Quickly calculate sum of any interval
  3. Divide and Conquer Strategy: Split problem into left and right parts
  4. DP State Definition: Clear state transition equations are crucial

Interview Tips

Common Pitfalls

❌ Forget that two subarrays must be non-overlapping
❌ Incorrect initialization of Kadane's algorithm
❌ Wrong boundary conditions for split points
✅ Use DP to preprocess left and right max values for O(n) complexity


🚀 Want to Excel in VO / OA? oavoservice Provides Comprehensive Interview Support

Whether it's OA assistance, real-time VO support, or interview coaching, our North American CS experts can provide real-time hints and strategies that far exceed AI capabilities.

Interview proxy, SDE interviews, FAANG interviews? No problem.

Using camera switching and voice-changing technology, our professional team completes your entire interview. With pre-simulation testing and perfect coordination, your face combined with our voice ensures confident and natural performance with seamless execution.

Guaranteed pass, Offer guaranteed, fast entry to top tech companies.

Whether it's OA, interviews, or offer negotiations, we provide full support until you secure your desired Offer.

Our service uses a small upfront deposit, final payment after receiving Offer model, so you have no worries.

We consistently provide professional online assessment and interview assistance services for major tech companies like Google, guaranteeing perfect scores and successful outcomes.

👉 Add WeChat: Coding0201

Secure your Google VO Offer opportunity!