What comes to your mind when you think about divide and conquer? Do you think about wars? A strategy? A way you can split up your forces and then conquer the enemy? I am here to tell you that is exactly right.

    Luckily, in computer science you don’t have to deal with weapons and artillery. There are several ways to approach a problem. Divide and conquer is one of them. In our case a problem statement is our enemy. We need to break it into parts (divide) and then solve (conquer) it. One may not realize, but you apply this algorithmic paradigm in day to day life even without realizing.

    So a divide and conquer strategy to solve a problem would involve breaking down a problem into smaller, easier parts that don’t require a lot of computation. Once these small problems are solved, we are able to conquer the original problem.

    How can I use it?

    Let us take up a real life example to understand it better. You are organizing a brunch party and have 8 guests coming over. You have a loaf of bread and you want to make equal partitions for every guest.

    PROBLEM: Given a loaf of bread, you need to divide it into 1/8th pieces, without using any measuring tape.

    figure showing slicing of bread in one-eighth pieces
    Fig: Slicing a bread into 1/8th pieces.

    How would you go about cutting this loaf of bread? If you are in the kitchen you would first divide the bread into half, to get 2 one-half pieces. You would again cut these pieces into 2 each to get 4 one-fourth pieces. You would repeat this process one more time with all these 4 pieces to get 8 one-eighth pieces. The approach you took would visually look something like this:

    cutting a loaf of bread using divide and conquer.
    Fig: Halving a bread piece at every level.

    As a human being we are better able to estimate half length rather than a 1/8th length. So we took advantage of this fact and kept on slicing the bread into half until we reached 1/8th lengths. We just used a divide a conquer approach, we divided our problem into sub-parts and solved it. One could argue that we can even directly slice the bread at 1/8th length, but human minds cannot calculate that easily.

    Okay, but what about the algorithm?

    In computer science, Binary Search is a very good example of the divide and conquer paradigm. We take advantage of the fact that the array is already sorted, and keep on dividing the problem into smaller parts.

    PROBLEM: Use binary search to check if the number 23 exists in the array.
    arr = {0, 4, 8, 15, 16, 23, 42, 99}

    • Just like the loaf of bread, we start from the middle of the array.
    • All elements smaller than equal to 15 are on the left part.
    • Since 23 is greater than 15, we can be pretty sure that we don’t need to search over there.
    • We look into the right part.
    • Again slice the array in 2 parts. All elements greater than equal to 42 are in the right part. Since 42 is greater than 23 we can ignore the right part of the array.
    • Look in the left part.
    • Again slice in 2 parts. Now we find our number and hence we can say that 23 exists in the array.
    using binary search to find element in an array
    Fig: Example of divide and conquer in a sorted array

    We divided the problem into smaller parts and then conquered it using some known truth (sorted array in this case). This is how a divide and conquer paradigm can be used to solve complex problems. Some examples where we use divide and conquer are:

    Video Explanation:
    YouTube player
    1 comment
    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Valid anagram strings

    by nikoo28
    5 minutes read

    Given two strings str1 and str2. Write a function to determine if str1 is an anagram of str2. Example 1:str1 = “listen”, str2 = “silent”Output = True Example 2:str1 = “mississippi”, str2 = “mips”Output = False Terminology: Anagram: Two words or phrases are said to be anagrams of each other if they can be formed by re-shuffling of characters in one of them. Problem Statement: In the given problem we have 2 strings as input. They may be single words or phrases. Return true if they anagrams of each other,…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    First unique character in a string

    by nikoo28
    4 minutes read

    You are given a string with some repeating characters and some unique characters. Find the first unique character and return its index. Example 1:Input: studyAlgorithmsOutput: 2 Example 2:Input: iLoveToCodeOutput: 0 Problem Statement: I want to simplify the problem statement first before we begin to solve it. An input string consists of some uppercase and lowercase characters which may or may not be repeated. Out of all the unique characters, you need to return the characters that occurs first in the string. In the first example studyAlgorithms, s, and t are…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Trees

    Diameter of a Binary Tree

    by nikoo28
    6 minutes read

    You are provided with a binary tree, return the length of the diameter. The diameter of the binary tree is defined as the length of the longest path between any two nodes in the tree. In the above example the diameter of the binary tree is 7. Problem Statement: We can easily find out the distance between two nodes in a binary tree. In the given example, the distance between node 5 and 8 is 4. The distance between node 2 and 6 is 3, the distance between node 8…

    1 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Maximum sum contiguous sub-array

    by nikoo28
    5 minutes read

    You are provided with an array of integers. We need to find the sub-array with the maximum sum such that all the elements are contiguous. Example:Input: [-2, -5, 6, -2, -3, 1, 5, -6]Output: 7Explanation: The sub-array with maximum sum is [6, -2, -3, 1, 5] Problem Statement: You are provided with an array of integers. These elements could be all positive, all negative or a combination of both. A sub-array is a smaller array formed using the elements of the original array. The condition for this problem is that…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Linked List

    Merge two sorted Linked Lists

    by nikoo28
    4 minutes read

    You are given 2 sorted linked lists with a head pointer. Find a way to merge these 2 lists and return a single sorted list. Try to solve the problem without using any extra space. Example 1:List 1: 1 -> 2 -> 4List 2: 1 -> 3 -> 4Output: 1 -> 1 -> 2 -> 3 -> 4 -> 4 Problem statement To read about Linked Lists, refer to the post “What is a Linked List“? For our current problem statement, we are given two list heads or we refer…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Single non-repeating number

    by nikoo28
    4 minutes read

    Let us suppose you are given an array of integers. The special thing about this array is that all numbers are repeated twice except one. Find the single non-repeating number. Example 1:Input: [2, 1, 1]Output: 2 Example 2:Input: [4, 5, 5, 2, 2]Output: 4 Problem statement: You are given an array of all positive integers. All the integers are repeated exactly twice except one. We need to find that number with a linear time complexity and using the minimum space possible. Brute Force Method: When tackling such problems, it is…

    1 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More