Faster Algorithm • Approach: divide and conquer • Divide the array in two parts, do each part separately • Divide using an appropriate power of 10 • Choose power of 10 that uses at least half of the bits 10
Faster Algorithm • 11
Faster Algorithm • 12
Exponential • 13
Divide and Conquer • 14
Time Complexity • 15
Stock trading problem • 16
Maximum Subarray • Let’s consider the daily changes (difference from previous day): • 13, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7 • Now the problem becomes: finding the range with largest sum • In the example: 18, 20, -7, 12; sum is 43 • How does that help? 17
Divide and Conquer • If we divide the array in two, a maximum subarray may be in: • The first half • The second half • In between (crossing division) • We can calculate the best option for each, then select the largest • First and second half can be solved by recursion • Crossing subarray can be found in linear time • Find largest sum that ends at midpoint • Find largest sum that starts just after midpoint 18