AfterAcademy Tech
•
16 Dec 2019

Divide and Conquer is a recursive problem-solving approach which break a problem into smaller subproblems, recursively solve the subproblems, and finally combines the solutions to the subproblems to solve the original problem. This method usually allows us to reduce the time complexity to a large extent.
We will be discussing the Divide and Conquer approach in detail in this blog. We will be exploring the following things:
It consists of three phases:
Problem Statement: Given a sorted array A[] of size n, you need to find if element K is present or not. If yes then return true otherwise return false.
Solution Idea: The naive solution for the problem do a linear search to check whether element K is present or not. This will take O(n) time complexity. But if we use the sorted property of the array, we can apply the divide and conquer approach to solve it efficiently in O(log n) time complexity.

The basic idea of binary search is to divide the array equally and compare the value K with the middle element. If A[mid] is greater than K then definitely K will not be present in the right part, so we search value K in the left part. Similarly, if A[mid] is less than K then we search value K in the right part. (Think!)
Solution Steps
Recursive Call : binarySearch(A[], l, r, k)
- if (A[mid] > k), then call binarySearch(A, l, mid-1, k)
- if (A[mid] < k), then call binarySearch(A, mid+1, r, k)
Complexity Analysis
On the basis of comparison with the middle value, we are reducing the input size by 1/2 at every step of recursion. In the worst case, Recursion will terminate at the base case which is l > r i.e the case of unsuccessful search. (Think!)
Suppose, T(n) = Time complexity of searching the value K in n size array. To summerise,
The recurrence relation for the above is: T(n) = T(n/2) + O(1)
Time complexity is O(log n), which is much faster than O(n) algorithm of linear search. Note: We can solve the above recurrence relation by recursion tree method or master theorem. (How? Think!)
Critical Ideas to think
Merge Sort is an efficient O(nlog n) sorting algorithm and It uses the divide-and-conquer approach. The algorithm works as follows:

Solution Steps
Recursive Call: mergeSort(A[], l, r)
1) Sort the first half: mergeSort(A, l, mid)
2) Sort the second half: mergeSort(A, mid+1, r)
merge(A, l, mid, r)
Complexity Analysis
Suppose, T(n) = Time complexity of searching the value K in N size array. To summerise,
The recurrence relation for the above is: T(n) = 2T(n/2) + O(n)
So, the time complexity of the merge sort is O(nlog n). Note: We can solve the above recurrence relation by recursion tree method or master theorem.
Critical Ideas to think!
Happy coding! Enjoy Algorithms!
AfterAcademy Tech
Divide and Conquer and Dynamic Programming are two most used problem-solving skills in Data Structures. In this blog, we will see the similarities and difference between Dynamic Programming and Divide-and-Conquer approaches.

AfterAcademy Tech
Dynamic Programming is one of the most commonly used approach of problem-solving during coding interviews. We will discuss how can we approach to a problem using Dynamic Programming and also discuss the concept of Time-Memory Trade-Off.

AfterAcademy Tech
The repeated execution of some groups of code statements in a program is called iteration. We will discuss the idea of iteration in detail in this blog.

AfterAcademy Tech
In this blog, we will see how to get started with Competitive Programming. Competitive Programming is very important because in every interview you will be asked to write some code. So, let's learn together.
