Overview
Divide and conquer is a fundamental algorithm design technique that solves problems by:- Dividing the problem into smaller subproblems
- Conquering each subproblem recursively
- Combining the solutions to solve the original problem
The divide and conquer technique is the foundation for many classic algorithms like Merge Sort, Quick Sort, and Binary Search.
How It Works
The divide and conquer strategy follows a three-step pattern:Common Examples
- Merge Sort: Divide array in half, sort each half, merge sorted halves
- Quick Sort: Partition around pivot, sort left and right partitions
- Binary Search: Divide search space in half each iteration
- Strassen’s Matrix Multiplication: Divide matrices into smaller blocks
Problem 1: Maximum Subarray Sum
Problem Description
Problem Description
Given an array that may contain both positive and negative integers, find the sum of the contiguous subarray with the largest sum.Example:
Solution Approach
Solution Approach
The divide and conquer approach considers three possibilities for the maximum subarray:
- It lies entirely in the left half
- It lies entirely in the right half
- It crosses the middle point
Implementation
Implementation
Complexity Analysis
Complexity Analysis
- Time Complexity: O(n log n)
- We divide the array into two halves: O(log n) levels
- At each level, we do O(n) work to find the crossing maximum
- Space Complexity: O(log n)
- Recursion stack depth
Problem 2: Inversion Count
Problem Description
Problem Description
Given an array of integers, find the inversion count. An inversion count indicates how far (or close) the array is from being sorted.Two elements
a[i] and a[j] form an inversion if a[i] > a[j] and i < j.Example:Solution Approach
Solution Approach
We can modify merge sort to count inversions efficiently. When merging two sorted subarrays:
- If an element from the right subarray is smaller than an element from the left subarray, it forms inversions with all remaining elements in the left subarray
- Count these inversions during the merge process
Implementation
Implementation
Complexity Analysis
Complexity Analysis
- Time Complexity: O(n log n)
- Same as merge sort - we’re essentially doing a merge sort while counting inversions
- Space Complexity: O(n)
- For the temporary arrays created during merging
When to Use Divide and Conquer
Use When
- Problem can be broken into similar subproblems
- Subproblems are independent
- Combining solutions is straightforward
- You need optimal time complexity
- Problem has natural recursive structure
Avoid When
- Subproblems overlap significantly (use DP instead)
- Base cases are too complex
- Overhead of recursion is too high
- Problem requires iterative solution
- Memory constraints are tight
Key Characteristics
Conquer
Solve the subproblems recursively. If subproblems are small enough, solve them directly (base case)
Tips and Best Practices
Comparison with Other Techniques
| Aspect | Divide and Conquer | Dynamic Programming |
|---|---|---|
| Subproblems | Independent | Overlapping |
| Approach | Top-down (recursive) | Bottom-up or top-down |
| Optimization | No memoization needed | Requires memoization |
| Examples | Merge sort, Binary search | Fibonacci, LCS |
Related Topics
Dynamic Programming
For overlapping subproblems
Greedy Algorithms
For optimization problems
Backtracking
For constraint satisfaction