We compare things all the time to determine which one is better over the other. The best example is when you are buying stuff online, we compare prices across websites, two different mobile phones, or even two different outfits. We know that there are different techniques of solving a problem in computer science: Brute Force Divide and Conquer Greedy Dynamic Programming Naturally, when we try to solve a problem with different methods, one would want to compare them. Once they are put side to side, we can then analyze which…
nikoo28
nikoo28
a techsavvy guy and a design buff... I was born with the love for exploring and want to do my best to give back to the community. I also love taking photos with my phone and Canon Kiss X5 in order to capture moments in my life. It's my pleasure to have you here.


Greedy algorithms is a paradigm that describes the approach on how you can go about solving a problem. You might have heard about a lot of algorithmic techniques while learning how to solve problems in computer science. The others being: Brute Force Divide and Conquer Dynamic Programming In this post we shall understand what a greedy algorithm is and how can it be used to solve problems which don’t seem very trivial at the first glance. What is a Greedy Algorithm? What is the first thing that comes to your…

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…

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…

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 reshuffling 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,…

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…

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…

You are provided with an array of integers. We need to find the subarray with the maximum sum such that all the elements are contiguous. Example:Input: [2, 5, 6, 2, 3, 1, 5, 6]Output: 7Explanation: The subarray 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 subarray is a smaller array formed using the elements of the original array. The condition for this problem is that…

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…

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 nonrepeating 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…