Skip to main content

Posts

LCM of two numbers

In this post we discuss a method to find out LCM (least common multiple) of 2 numbers, LCM of 2 numbers is the least number that is divisible by 2 numbers. for example, if we consider 4 and 5 then their LCM is 20. Though there are a lot of methods to find LCM of 2 numbers here I will focus on find LCM using GCD of 2 numbers. Consider 'A' and 'B' to be 2 numbers so we calculate LCM  by using the below formula The product of the numbers is equal to the product of their GCD and LCM.  A * B = GCD(A,B) * LCM(A,B) If you want to know about GCD of 2 numbers then follow my post on GCD Here. In the following programs, we first find the GCD of 2 numbers and then divide the product of two numbers by the GCD to LCM of the numbers. C Program C++ Program Java program Python Program C Program C++ Program Java Program Python Program You might also be interested in  Anagram Strings Double Linked List Finding Middle node ...

Check is given strings are Anagrams

Checking if 2 given strings are anagrams or not is one of the most common and easiest interview questions which is often asked in interview coding rounds. In this post we discuss method to identify if 2 strings are anagrams or not. 2 strings or words or phrases are said to be Anagrams if they are formed by rearrangement of same letters. For example we consider "HOST" and "SHOT" they are formed by rearrangement of letters H,O,S,T. We can find a lot of such examples. Let us now discuss the method to check if 2 strings are anagrams. First we remove all the white spaces from the given strings. For strings to be anagrams the length of the strings after removing white spaces must be same, so we compare lengths. if the lengths are equal then we move forward. else we stop as the length of the strings must be same. The next step is to convert the string to lowercase, this step is optional and you can choose not to do it depending on your requirement. Then ...

Binary Search Algorithm

Binary Search also known as Algorithmic or binary chop is a searching algorithm that searches for a element in a sorted data structure. Binary search is a searching algorithm that searches the required value by comparing the target value with the middle value of the array. The following is the algorithm for binary search implementation Step 0 : Sort the array using any sorting algorithm (I am using bubble sort in the below program) Step 1 : Initialize first = 0 and last  = list.size() - 1 Step 2 :  while first < last and the element is not found calculate mid = (first + last) / 2 Step 3 : Compare target value with middle element Step 4 : if the middle value is greater than the target value then target value lies on the left side of the middle value do goto step 1 with last = mid - 1 Step 5 : if the middle value is lesser than the target value then target value lies on the right side of the middle value do goto step 1 with first = mid + 1 Step 6 : ...

Finding Middle Node in Linked List

Linked List is dynamic data structure that is used to store information. Unlike arrays linked lists are not stored in contagious memory locations rather the are stored at any empty place in memory and the address of the next node is stored in the link field.  Hence we don't have direct access to the Linked List elements as we do in arrays. In order to find the middle node of the Linked List there are 2 methods Method 1 :  Traverse the Linked list count the number of nodes and then move a pointer to that node by using a counter. This method although useful requires us to traverse the List 2 times. Method 2 :  Use 2 pointers (say) fast and slow starting from the head node increment the fast pointer by 2 and increment the slow pointer by 1. In this way when the fast pointer reaches the end the slow pointer will point to the middle element. To know more about Linked List and its implementation click Here . The following is the implementation of the Met...

Mid-point Circle Generation Algorithm

Mid-Point circle generation algorithm aims to find out the points that lie on the circle (or approximately lie on the circle) in a pixel based display. This Algorithm is similar to Bresenhams Circle Generation Algorithm , According to wikipedia bresenhams algorithm is derived from mid-point circle generation algorithm. In this algorithm we divide the circle into 8 parts and calculate the points only for one part and then apply the property of symmetry to get the points in the remaining 7 parts. We divide the circle into the following parts. At the beginning we start with the point x = 0, y = r ; so we get the first point as (xc + x, yc + y) which lies on the circle. let us denote this point as (xp, yp). now we have check if the neighbouring point (xp - 1, yp + 1) or (xp, yp + 1) lies on the circle, We do this by checking the value of the variable called parameter which checks if the point lies on the circle or not and accordingly we update the value of x and y to plo...

Bresenhams Circle Generation Algorithm

Bresenhams circle generation algorithm aims to find out the points that lie on the circle (or approximately lie on the circle) in a pixel based display. In this algorithm we divide the circle into 8 parts and calculate the points only for one part and then apply the property of symmetry to get the points in the remaining 7 parts. We divide the circle into the following parts. At the beginning we start with the point x = 0, y = r ; so we get the first point as (xc + x, yc + y) which lies on the circle. let us denote this point as (xp, yp). now we have check if the neighbouring point (xp - 1, yp + 1) or (xp, yp + 1) lies on the circle, We do this by checking the value of the variable called parameter which checks if the point lies on the circle or not and accordingly we update the value of x and y to plot the next point. Algorithm Plot the initial point with x = 0, y = r and initialize parameter = 3 - 2*radius While x < y keep repeating steps 3, 4 and 5 If...

Infix to Prefix conversion using Stack

This post is about conversion of Infix expression to Prefix conversion. For this conversion we take help of stack data structure, we need to push and pop the operators in and out of the stack. Infix expressions are the expressions that we normally use, eg. 5+6-7; a+b*c etc. Prefix expressions are the expressions in which the 2 operands are preceded by the operator eg. -+56 7 , +a*bc etc. This method is very similar to the method that we used to convert Infix to Postfix but the only difference is that here we need to reverse the input string before conversion and then reverse the final output string before displaying it. NOTE: This changes one thing that is instead of encountering the opening bracket we now first encounter the closing bracket and we make changes accordingly in our code. So, to convert an infix expression to a prefix expression we follow the below steps (we have 2 string, 1st is the input infix expression string 2nd is the output string which is empty initially)...