0% found this document useful (0 votes)
33 views3 pages

L3 Divide and Conquer

The document discusses two algorithms: the Divide and Conquer approach for the Maximum Subarray Problem, which finds the contiguous subarray with the maximum sum, and the Karatsuba algorithm for faster integer multiplication. The Maximum Subarray Problem is solved by dividing the array, conquering the left and right halves, and combining results, yielding a maximum subarray of 18,20,−7,12 with a sum of 43. The Karatsuba algorithm reduces multiplication of large integers into three smaller multiplications, resulting in an efficient computation of 1234×5678=7006652.

Uploaded by

pushkar.pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

L3 Divide and Conquer

The document discusses two algorithms: the Divide and Conquer approach for the Maximum Subarray Problem, which finds the contiguous subarray with the maximum sum, and the Karatsuba algorithm for faster integer multiplication. The Maximum Subarray Problem is solved by dividing the array, conquering the left and right halves, and combining results, yielding a maximum subarray of 18,20,−7,12 with a sum of 43. The Karatsuba algorithm reduces multiplication of large integers into three smaller multiplications, resulting in an efficient computation of 1234×5678=7006652.

Uploaded by

pushkar.pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Divide and Conquer: Maximum Subarray Problem

Problem:

Given an array of integers (which can include negative values), find the contiguous
subarray that has the maximum sum.

Example Problem:

Input array: 13,−3,−25,20,−3,−16,−23,18,20,−7,12,−5,−22,15,−4,7

Divide and Conquer Approach:

1. Divide: Split the array into two halves.


2. Conquer: Solve the problem recursively for the left and right halves.
3. Combine: Find the maximum subarray that crosses the midpoint.

The solution combines:

1. Maximum subarray in the left half.


2. Maximum subarray in the right half.
3. Maximum subarray that crosses the midpoint.

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:

 Left half: 13,−3,−25,20,−3,−16,−23,18


 Right half: 20,−7,12,−5,−22,15,−4,7

Step 3: Conquer and Combine

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.

Karatsuba Faster Integer Multiplication Algorithm

Problem:

Multiply two large integers X and Y.

Algorithm:

The Karatsuba algorithm reduces the multiplication of two n-digit numbers into three
multiplications of approximately n/2 digit numbers, significantly improving efficiency.

1. Split XXX and YYY into halves:


o X=10m⋅A+B
o Y=10m⋅C+D
(m is the size of the split)
2. Compute the following recursively:
o P=A×C
o Q=B×D
o R=(A+B)×(C+D)
3. Combine results:
o X×Y=P⋅102m+(R−P−Q)⋅10m+Q

Numerical Solution:

Example: Multiply 1234 and 5678.

Step 1: Split Numbers

 X=1234=12⋅100+34, so A=12, B=34


 Y=5678=56⋅100+78, so C=56, D=78

Step 2: Recursively Compute

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

Step 3: Combine Results


Using the formula:
X×Y=P⋅102m+(R−P−Q)⋅10m+Q
 10m=102=100
 Combine:

1234×5678=672⋅104+(6164−672−2652)⋅102+2652=6720000+284000+2652=7006652

Result: 1234×5678=7006652.

You might also like