L3 Divide and Conquer
L3 Divide and Conquer
Problem:
Given an array of integers (which can include negative values), find the contiguous
subarray that has the maximum sum.
Example Problem:
Numerical Solution:
Step 1: Input
Array = 13,−3,−25,20,−3,−16,−23,18,20,−7,12,−5,−22,15,−4,7
Step 2: Divide
Split the array recursively:
1. Left Half
Maximum subarray in the left half is 18 (sum = 18).
2. Right Half
Maximum subarray in the right half is 15,−4,7 (sum = 18).
3. Crossing Subarray
Maximum crossing subarray: Combine 18 from the left and 20,−7,12 from the right,
resulting in 18,20,−7,12 (sum = 43).
Result: The maximum subarray is 18,20,−7,12 with a sum of 43.
Problem:
Algorithm:
The Karatsuba algorithm reduces the multiplication of two n-digit numbers into three
multiplications of approximately n/2 digit numbers, significantly improving efficiency.
Numerical Solution:
1. P=A×C=12×56=672
2. Q=B×D=34×78=2652
3. R=(A+B)×(C+D)=(12+34)×(56+78)=46×134=6164
1234×5678=672⋅104+(6164−672−2652)⋅102+2652=6720000+284000+2652=7006652
Result: 1234×5678=7006652.