SlideShare a Scribd company logo
Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU.
Divide and Conquer
▪ It is a fundamental algorithm design paradigm with three key steps:
1. Divide the problem into a number of subproblems that are smaller instances of the same problem.
2. Conquer the subproblems by solving them recursively.
3. Combine the solutions to the subproblems into the solution for the original problem.
▪ The base cases for the recursion are subproblems of constant size (trivial to solve).
P1 – Merge Sort
Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU.
Algorithm:
P2 – Quick Sort
Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU.
Partition Process:
Algorithm:
Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU.
Practices:
1. Calculate pow(x,n)
Given, the value of x and n. You will calculate the value of xn
using the divide and conquer.
Approach:
If n is an even number, then your result is pow(x, n/2)*pow(x, n/2)
If n is an odd number, then your result is pow(x,n/2)*pow(x,n/2)*x
If n is 0, then your result is 1.
2. Randomized Binary Search
Given a sorted array of n numbers and a search value x, you need to search for x from the sorted array.
Normally, during binary search we always consider the middle element for our search, if not found then we decide
to search the left subarray or the right subarray.
But during the randomized binary search, we will randomly pick an element (instead of the middle element) for our
search and if not found then we will decide to search in the left subarray or the right.
3. Fast multiplication algorithm (Karatsuba Algorithm)
Old school method – O(n2
)
Karatsuba Algorithm- O(n1.59
)
Number 1: 12345 = 10m
* 12 + 345 = 10m
a + b
Number 2: 56789 = 10m
* 56 + 789 = 10m
c + d
here, m = ceil( number of digits/2 ) = 3
Multiplication:
12345 * 56789
= (10m
* a + b) * ( 10m
* c + d)
= 102m
* ac + 10m
* (ad + bc) + bd
= 102m
* ac + 10m
* (ac + bd – (a-b)*(c-d)) + bd
4. Count inversions in an array
Inversion count for an array indicates – how far the array is from being sorted. If the array is already sorted, then
the inversion count is 0, but if the array is sorted in the reverse order, the inversion count is the maximum.
For example,
Input: arr[]={9, 5, 3, 1} Output: 6
The inversions are: (9,5), (9,3), (9,1), (5,3), (5,1), (3,1)
Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU.
5. Maximum subarray sum
Given a one-dimensional array that may contain both positive and negative integers, find the sum of continuous
subarray of numbers which has the largest sum.
For example,
Approach:
1. Divide the array into two halves
2. Return the maximum of the following 3:
a) Maximum subarray sum in left half (recursive call)
b) Maximum subarray sum in right half (recursive call)
c) Maximum subarray sum such that the subarray crosses the midpoint (process yourself)
6. Rotation count in rotated sorted array
Given a rotated sorted array of distinct integers, your task is to find out the total number of rotations.
For example,
{ 15, 17, 1, 2, 6, 11 } is rotated 2 times.
{ 7, 9, 11, 12, 5 } is rotated 4 times.
In short, the index of the smallest integer is the rotation count. Minimum element is the one whose previous
element is greater than it. If there is no previous element, then there is no rotation. First check the middle element,
if it is not the minimum element then decide whether to search the left subarray or the right.
7. Find a peak element
Given an array of integers, find a peak element in it. An array element is a peak if it is not smaller than its
neighbors. For corner element, only consider one neighbor.
For example,
In { 5, 10, 15, 12, 20, 25 }, 15 is the peak element.
Similar to binary search, first check whether the middle element is the peak element or not. If the middle element is
not the peak element, then check if the element on the right side is greater than the middle element then there is
always a peak element on the right side. Similarly, if the element on the left side is greater than the middle element
then there is always a peak element on the left side.

More Related Content

Similar to DS & Algo 3 - Divide and Conquer (20)

PPTX
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
madhu614742
 
PPTX
Array ADT(Abstract Data Type)|Data Structure
Akash Gaur
 
PPT
MergesortQuickSort.ppt
AliAhmad38278
 
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
PPT
3-Chapter Three - Divide and Conquer.ppt
mershaabdisa
 
PDF
Essential Problems Every Developer Should Know in Data Structures and Algorithms
KantubhukthaJanardha
 
PPTX
Module 2_ Divide and Conquer Approach.pptx
nikshaikh786
 
PDF
Divide and conquer
Emmanuel college
 
PPTX
Program Practical to operations on Array
bhargavidalal5
 
PPTX
Algorithm & data structures lec4&5
Abdul Khan
 
PDF
450 dsa (1).pdf
karansharma193921
 
PPTX
Design and Analysis of Algorithms Lecture Notes
Sreedhar Chowdam
 
PPTX
Algorithms - Rocksolid Tour 2013
Gary Short
 
PPTX
Advanced Algorithms: Transform and conquer
Abdulrazak Zakieh
 
PDF
Chapter 9 divide and conquer handouts with notes
mailund
 
PPT
Introduction to basic algorithm knowledge.ppt
Adrianaany
 
PDF
Recursion Lecture in Java
Raffi Khatchadourian
 
PPTX
Introduction to Algorithms
pppepito86
 
PPTX
Counting Sort
Faiza Saleem
 
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
madhu614742
 
Array ADT(Abstract Data Type)|Data Structure
Akash Gaur
 
MergesortQuickSort.ppt
AliAhmad38278
 
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
3-Chapter Three - Divide and Conquer.ppt
mershaabdisa
 
Essential Problems Every Developer Should Know in Data Structures and Algorithms
KantubhukthaJanardha
 
Module 2_ Divide and Conquer Approach.pptx
nikshaikh786
 
Divide and conquer
Emmanuel college
 
Program Practical to operations on Array
bhargavidalal5
 
Algorithm & data structures lec4&5
Abdul Khan
 
450 dsa (1).pdf
karansharma193921
 
Design and Analysis of Algorithms Lecture Notes
Sreedhar Chowdam
 
Algorithms - Rocksolid Tour 2013
Gary Short
 
Advanced Algorithms: Transform and conquer
Abdulrazak Zakieh
 
Chapter 9 divide and conquer handouts with notes
mailund
 
Introduction to basic algorithm knowledge.ppt
Adrianaany
 
Recursion Lecture in Java
Raffi Khatchadourian
 
Introduction to Algorithms
pppepito86
 
Counting Sort
Faiza Saleem
 

More from Mohammad Imam Hossain (20)

PDF
DS & Algo 6 - Offline Assignment 6
Mohammad Imam Hossain
 
PDF
DS & Algo 6 - Dynamic Programming
Mohammad Imam Hossain
 
PDF
DS & Algo 5 - Disjoint Set and MST
Mohammad Imam Hossain
 
PDF
DS & Algo 4 - Graph and Shortest Path Search
Mohammad Imam Hossain
 
PDF
DS & Algo 2 - Offline Assignment 2
Mohammad Imam Hossain
 
PDF
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
PDF
DS & Algo 1 - Offline Assignment 1
Mohammad Imam Hossain
 
PDF
DS & Algo 1 - C++ and STL Introduction
Mohammad Imam Hossain
 
PDF
DBMS 1 | Introduction to DBMS
Mohammad Imam Hossain
 
PDF
DBMS 10 | Database Transactions
Mohammad Imam Hossain
 
PDF
DBMS 3 | ER Diagram to Relational Schema
Mohammad Imam Hossain
 
PDF
DBMS 2 | Entity Relationship Model
Mohammad Imam Hossain
 
PDF
DBMS 7 | Relational Query Language
Mohammad Imam Hossain
 
PDF
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
PDF
DBMS 5 | MySQL Practice List - HR Schema
Mohammad Imam Hossain
 
PDF
TOC 10 | Turing Machine
Mohammad Imam Hossain
 
PDF
TOC 9 | Pushdown Automata
Mohammad Imam Hossain
 
PDF
TOC 8 | Derivation, Parse Tree & Ambiguity Check
Mohammad Imam Hossain
 
PDF
TOC 7 | CFG in Chomsky Normal Form
Mohammad Imam Hossain
 
PDF
TOC 6 | CFG Design
Mohammad Imam Hossain
 
DS & Algo 6 - Offline Assignment 6
Mohammad Imam Hossain
 
DS & Algo 6 - Dynamic Programming
Mohammad Imam Hossain
 
DS & Algo 5 - Disjoint Set and MST
Mohammad Imam Hossain
 
DS & Algo 4 - Graph and Shortest Path Search
Mohammad Imam Hossain
 
DS & Algo 2 - Offline Assignment 2
Mohammad Imam Hossain
 
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
DS & Algo 1 - Offline Assignment 1
Mohammad Imam Hossain
 
DS & Algo 1 - C++ and STL Introduction
Mohammad Imam Hossain
 
DBMS 1 | Introduction to DBMS
Mohammad Imam Hossain
 
DBMS 10 | Database Transactions
Mohammad Imam Hossain
 
DBMS 3 | ER Diagram to Relational Schema
Mohammad Imam Hossain
 
DBMS 2 | Entity Relationship Model
Mohammad Imam Hossain
 
DBMS 7 | Relational Query Language
Mohammad Imam Hossain
 
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
DBMS 5 | MySQL Practice List - HR Schema
Mohammad Imam Hossain
 
TOC 10 | Turing Machine
Mohammad Imam Hossain
 
TOC 9 | Pushdown Automata
Mohammad Imam Hossain
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
Mohammad Imam Hossain
 
TOC 7 | CFG in Chomsky Normal Form
Mohammad Imam Hossain
 
TOC 6 | CFG Design
Mohammad Imam Hossain
 
Ad

Recently uploaded (20)

PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Dimensions of Societal Planning in Commonism
StefanMz
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Ad

DS & Algo 3 - Divide and Conquer

  • 1. Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU. Divide and Conquer ▪ It is a fundamental algorithm design paradigm with three key steps: 1. Divide the problem into a number of subproblems that are smaller instances of the same problem. 2. Conquer the subproblems by solving them recursively. 3. Combine the solutions to the subproblems into the solution for the original problem. ▪ The base cases for the recursion are subproblems of constant size (trivial to solve). P1 – Merge Sort
  • 2. Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU. Algorithm: P2 – Quick Sort
  • 3. Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU. Partition Process: Algorithm:
  • 4. Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU. Practices: 1. Calculate pow(x,n) Given, the value of x and n. You will calculate the value of xn using the divide and conquer. Approach: If n is an even number, then your result is pow(x, n/2)*pow(x, n/2) If n is an odd number, then your result is pow(x,n/2)*pow(x,n/2)*x If n is 0, then your result is 1. 2. Randomized Binary Search Given a sorted array of n numbers and a search value x, you need to search for x from the sorted array. Normally, during binary search we always consider the middle element for our search, if not found then we decide to search the left subarray or the right subarray. But during the randomized binary search, we will randomly pick an element (instead of the middle element) for our search and if not found then we will decide to search in the left subarray or the right. 3. Fast multiplication algorithm (Karatsuba Algorithm) Old school method – O(n2 ) Karatsuba Algorithm- O(n1.59 ) Number 1: 12345 = 10m * 12 + 345 = 10m a + b Number 2: 56789 = 10m * 56 + 789 = 10m c + d here, m = ceil( number of digits/2 ) = 3 Multiplication: 12345 * 56789 = (10m * a + b) * ( 10m * c + d) = 102m * ac + 10m * (ad + bc) + bd = 102m * ac + 10m * (ac + bd – (a-b)*(c-d)) + bd 4. Count inversions in an array Inversion count for an array indicates – how far the array is from being sorted. If the array is already sorted, then the inversion count is 0, but if the array is sorted in the reverse order, the inversion count is the maximum. For example, Input: arr[]={9, 5, 3, 1} Output: 6 The inversions are: (9,5), (9,3), (9,1), (5,3), (5,1), (3,1)
  • 5. Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU. 5. Maximum subarray sum Given a one-dimensional array that may contain both positive and negative integers, find the sum of continuous subarray of numbers which has the largest sum. For example, Approach: 1. Divide the array into two halves 2. Return the maximum of the following 3: a) Maximum subarray sum in left half (recursive call) b) Maximum subarray sum in right half (recursive call) c) Maximum subarray sum such that the subarray crosses the midpoint (process yourself) 6. Rotation count in rotated sorted array Given a rotated sorted array of distinct integers, your task is to find out the total number of rotations. For example, { 15, 17, 1, 2, 6, 11 } is rotated 2 times. { 7, 9, 11, 12, 5 } is rotated 4 times. In short, the index of the smallest integer is the rotation count. Minimum element is the one whose previous element is greater than it. If there is no previous element, then there is no rotation. First check the middle element, if it is not the minimum element then decide whether to search the left subarray or the right. 7. Find a peak element Given an array of integers, find a peak element in it. An array element is a peak if it is not smaller than its neighbors. For corner element, only consider one neighbor. For example, In { 5, 10, 15, 12, 20, 25 }, 15 is the peak element. Similar to binary search, first check whether the middle element is the peak element or not. If the middle element is not the peak element, then check if the element on the right side is greater than the middle element then there is always a peak element on the right side. Similarly, if the element on the left side is greater than the middle element then there is always a peak element on the left side.