Maximum Subarray Divide and Conquer
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
The maximum subarray problem is a classic problem in computer science, often serving as an introduction to various algorithmic paradigms such as divide-and-conquer, dynamic programming, and greedy algorithms. The problem statement is straightforward: given an array of integers, find the contiguous subarray with the highest possible sum.
This article delves into solving the maximum subarray problem using the divide-and-conquer approach, enriched with technical explanations, examples, and summaries.
Understanding the Problem
Given an array A
of size n
, the task is to identify the subarray with the largest sum among all possible contiguous subarrays.
For instance, consider the array A = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
. The maximum subarray is [4, -1, 2, 1]
with a sum of 6.
Divide and Conquer Approach
The principle of divide and conquer involves dividing a problem into subproblems of smaller size, solving these subproblems recursively, and combining their solutions to solve the overarching problem.
Steps in Divide and Conquer
- Divide: Split the array into two halves.
- Conquer: Recursively find the maximum subarray in both halves.
- Combine: Combine the results of the two halves to find the maximum subarray that crosses the midpoint.
Combining the Results
The subarray with the maximum sum might fall into one of three categories:
- Completely in the left subarray.
- Completely in the right subarray.
- Crossing the midpoint, with some elements from the left and some from the right.
The third option involves computing max_crossing_subarray
, which includes:
- A prefix sum from the midpoint to the left.
- A suffix sum from the midpoint to the right.
Algorithm Implementation
Here’s a Python implementation for the above algorithm.
- Divide and Conquer: Typically, the time complexity is . This stems from the fact that we divide the problem into two halves recursively, creating a balanced tree structure.
- Intuitive understanding of recursion and problem splitting.
- Demonstrates how recurrence relations work in complex problems.
- Complexity is higher compared to dynamic programming (Kadane's Algorithm).
- Overhead due to recursion calls might be inefficient with large input sizes.
Related reading
- Maximum subarray sum modulo M
- maximum subarray whose sum equals 0
- Maximum sum of non consecutive elements
- Maximum Sum SubArray
- Maximum sum sublist?
- maximum sum subrectangle in a sparse matrix
- Maximum weighted bipartite matching, constraint ordering of each graph is preserved
- Maximum xor among all subsets of an array

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.