Algorithmic Paradigms – Brute Force

    by nikoo28

    Brute force method would probably be the favorite algorithmic paradigm of every developer. The beauty of a brute force method is that it is pretty straightforward and if a solution to a problem exists, you are guaranteed to find it. This option is also the most exhaustive option as we might go through all the possibilities before arriving at the result.

    What is Brute Force Method?

    You might have heard the phrase “Brute force method to solve the problem”. What exactly do you mean by that? It simply means that try out all the alternatives until you are exhausted of options. If the problem is solvable, you will eventually find an answer.

    fig showing representation of brute force method
    Fig: Example of brute force attack

    Let us understand it with a very easy example. Suppose you have a locked folder that can be opened by a 5-digit lock code. You have no hint about the lock code, but you just want to see what is behind that lock, what would you do? You would start from 00000 and go all the way to 99999. If the lock is working correctly, one of the combination would definitely open the lock. Although it would take some time, but eventually you would be able to solve the problem.

    What you just did was a Brute force solution. On the other hand, if you have hints about who owns the folder, or what date was the folder created, you may be able to try out some known combinations first. That could be an optimized approach. Similarly, for problem solving skills in computer science, if a solution to a problem exists, it guaranteed, that you will find it by a brute force method, i.e. applying all the possible combinations.

    What do we mean by “if a solution exists”?

    In the above example you may have noticed words like, “if the lock is working correctly” and “if a solution exists“. This statement is very essential when approaching any problem. Let us try to understand what do we mean by this.

    QUESTION: Given a 3 * 3 grid, you need to fill in numbers from 1-9 such that sum of all diagonals, rows and columns is the same. Also note that none of the numbers should be repeated.

    empty 3 by 3 grid
    Fig: 3 * 3 grid to be filled with numbers 1-9

    We also call it the magic square problem. Now you have to approach this problem in a brute force manner. You can start of by trying numbers in any fashion, and keep on verifying the sum in all directions.

    image showing different examples of 3 by 3 grid
    Fig: Trying out different combinations in 3 * 3 grid

    Eventually you will reach the correct answer. It is shown in the figure below. The sum of every row, column and diagonal is equal to 15 in this combination of numbers. Because a solution to this problem existed, we were able to find it in a brute force manner. But do keep in mind that the brute force method to solve this problem could be very time taking. There are a total of 362880 total possible ways to fill up the grid.

    3 by 3 grid solved
    Fig: 3 * 3 grid solution

    Important! Let us try to change the question a little bit. Instead of a 3 * 3 grid, what if you have a 2 * 2 grid? You need to fill the grid with numbers 1-4 such that no numbers are repeated. No matter what combination you try, you can never reach a condition where the sum of all rows, diagonals and columns is the same.

    figure showing all possibilities of 2 by 2 grid
    Fig: Examples of 2 * 2 grid combinations

    This means that the solution to this problem does not exist. Hence, even a brute force approach would be unsuccessful.

    Summary:

    This is what makes the brute force paradigm so popular and one of the favorites. It gives a sure shot way to determine if a problem is solvable. Usually developers try to approach the problem with a brute force approach first, if you are able to verify that a solution exists, only then you go ahead and try to optimize it.

    You can have a look at some of the problems where we discuss a brute force method first, before we start to optimize the solution:

    Video Explanation:
    YouTube player
    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • 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 …

    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

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