### Divide and Conquer

Divide and Conquer is a very popular algorithm that many people use to solve problems. With Divide and Conquer, a problem in hand, is divided into smaller sub-problems and then each problem is solved independently. When we keep on dividing the subproblems into even smaller sub-problems, we may eventually reach a stage where no more division is possible. Those smallest sub-problem are solved. The solution of all sub-problems is finally merged in order to obtain the solution of the original problem.

Keep in mind that the divide and conquer algorithm is founded on recursion. It involves understanding a problem, separating it into subproblems, and combining the solutions to solve the larger problem just like recursion.

### Divide and Conquer Strategy

1. Divide – breaking the problem into smaller sub-problems of the same problem
2. Conquer – solve the problem if it’s small enough(base case). If the problem is still not small enough, divide it and recursively solve its sub-problems. This step receives a lot of smaller sub-problems to be solved. Generally, at this level, the problems are considered ‘solved’ on their own.
3. Combine – combine solutions from sub problems to generate the solution for the original problem.
```/**
* Recursion used for factorial<br>
* Start from the top and go to bottom, then calculation will start from bottom back to top
*/
private static int factorial(int num) {
/**
* validation
*/
if (num < 0) {
throw new IllegalArgumentException("num is invalid");
}

/**
* base case, solve problem when it's small enough
*/
if (num == 0) {
return 1;
}

/**
* recursive case, divide into sub-problems
*/

return num * factorial(num - 1);
}``` A problem can be also like this. What are the disadvantages of Divide and Conquer?

Divide and Conquer algorithm requires recursion, which takes up more space than other processes. Your recursion stacks may overflow, causing the solution to fail.

Choosing base cases may also pose problems. Small, simple base cases typically lead to easier problems and solutions, while large base cases usually improve efficiency.

What are the advantages of Divide and Conquer?

Divide and Conquer algorithm helps solve complex problems relatively quickly and with much less effort(lines of code). It works well with processors because of their parallel structure. It also allows for easy storing and accessing by memory caches.

### Where is Divide and Conquer being used?

Divide and Conquer is being used in these algorithms:

• Merge Sort
• Quick Sort
• Binary Search

This method usually allows us to reduce the time complexity by a large extent.

For example, Bubble Sort uses a complexity of `O(n^2)`, whereas quicksort (an application Of Divide And Conquer) reduces the time complexity to `O(nlog(n))`. Linear Search has time complexity `O(n)`, whereas Binary Search (an application Of Divide And Conquer) reduces time complexity to `O(log(n))`.

When do you use Divide and Conquer?

Divide and Conquer should be used when same subproblems are not evaluated many times.

For example, Binary Search is a Divide and Conquer algorithm, we never evaluate the same subproblems again.

Here is an article that really caught my attention. It talks about how to use Divide and Conquer in business.

Source code on Github
{"ft_lang_label":"__label__en","ft_lang_prob":0.88670987,"math_prob":0.9194961,"size":2636,"snap":"2023-40-2023-50","text_gpt3_token_len":543,"char_repetition_ratio":0.15843464,"word_repetition_ratio":0.037647057,"special_character_ratio":0.21016692,"punctuation_ratio":0.09808102,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9968319,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-01T19:46:58Z\",\"WARC-Record-ID\":\"<urn:uuid:5fe6ff9f-c3ff-487b-8211-0d5dcbd2b602>\",\"Content-Length\":\"54946\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:26667b1c-a651-4a9d-a551-9386414799d6>\",\"WARC-Concurrent-To\":\"<urn:uuid:be101f31-ef9c-47ba-9723-f54a9995395a>\",\"WARC-IP-Address\":\"107.180.246.32\",\"WARC-Target-URI\":\"https://lovemesomecoding.com/data-structure-algorithm/data-structure-algorithm-divide-and-conquer\",\"WARC-Payload-Digest\":\"sha1:PI4FIOFBZDIY73TVIADLRVFMZ7FPMRDU\",\"WARC-Block-Digest\":\"sha1:OWHQSAITIVNYH4O4ZNF6HIA4QZP37AFZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100304.52_warc_CC-MAIN-20231201183432-20231201213432-00727.warc.gz\"}"}