Skip to main content

Posts

MO’s algorithm and its applications

  Before learning MO’s Algorithm, let us take a quick look at the prerequisites through which we can solve it. In  competitive programming , we come across questions which involves queries mostly static queries which are given as vectors or  arrays . Each such queries represent a run in the code. So, do you think running the code each time for every query operation is fine? Obviously, it is a brute force approach and it will fail most of the times in a coding platform as there will be a huge number of queries. So, what we can do? Can we always break up the array in two parts for like we do in  Binary Search ? Not really! And nor it will be beneficial. Let us consider a particular part of the array may be some  x th  part  of the array size. We divide the array into x parts to reduce our time complexity. By using complex mathematical calculations – differentiation we found out that this x is nothing but, where n is the size of the array. We come to the conclusion that dividing the array
Recent posts

Explained: Euclid’s GCD Algorithm

One of the earliest known numerical algorithms is that developed by Euclid (the father of geometry) in about 300 B.C. for computing the Greatest Common Divisor (GCD) of two positive integers. Euclid’s algorithm is an efficient method for calculating the GCD of two numbers, the largest number that divides both of them without any remainder. Algorithm: The first way of doing it if we try to subtract the smallest number from the greatest, GCD remain the same and in this way, if we keep repeating this step we’ll finally get  GCD . But as above process subtraction could be time-consuming then instead of subtracting what we do, we divide the smaller number, the algorithm stops when we find remainder 0. Let GCD(x,y) be the GCD of positive  integers  x and y. If x = y, then obviously GCD(x,y) = GCD(x,x) = x Euclid’s insight was to observe that, if x > y, then GCD(x,y) = GCD(x-y,y). Actually, this is easy to prove. Suppose that d is a divisor of both x and y. Then there exist integers q 1  a

STL-3

  Most coders use  C++  language for competitive programming. It is used widely because of its reliability, faster execution, short snippets, etc. Easy availability of the tutorials in c++ makes it more adaptive by coders. C++ STL is the backbone of programming. The inbuilt functions reduce a code snippet to even a single line sometimes. Example:  To sort an  array , no need to write the whole algorithms of sorting. A function does all the magic here which is known as “sort” function. Now let’s look at some interesting facts about  STL  and c++ programming. > GCD function GCD of two numbers is found with __gcd(a,b) where a and b are two numbers. No need to write the Euclidean algorithm. The function returns an integer, the GCD. Note – This function works only in GCC. Example- include<bits/stdc++.h> using namespace std; int main() { cout << __gcd(45,15); // outputs 15 as GCD return 0; } Sometimes it is messy to convert an int to a string and vice versa which at times give