Saturday 10 December 2016

Lets Get Started! - What is Binary Search?

What will you do if asked to search for an element in an array of integers. As a naive approach you will traverse the element one by one and check each element if it is equal to the to be searched element. 
Now thinking in a different way if we are given a sorted array(increasing order) and we are asked to do the same task. What can be done is, we will first divide the array into two halves , low and high using the mid element. Now we compare the element to be searched (assume it to be x) to the element at index=mid. If arr[mid]==x, we get our answer, but if that is not true, we divide the array in two halves the upper half and the lower half. As the array is already sorted, the element will either occur in the lower or upper half of the array, ie , either to the right or left of mid element. We will eliminate the other choice as the element 'x' cannot lie in that interval. For eg.

Given an array={2,4,5,8,9}, and x=4, here n=5 and mid=n/2=2th index. Now arr[2]==5 which is not equal to 4 , therefore we proceed further and eliminate the upper half of the array, i.e ,[mid+1,high] as 4 cannot lie between 8 and 9. And thus we move to lower half.

Now once we have chosen the half we need to move into, apply the steps again, taking low=low,high=mid-1, mid=(low+high)/2, and repeat the steps until you find the number or the array could not be further divided into halves.

The time complexity for the algorithm is O(log N) as it divides the array into halves and eliminated the other half.
The algorithm can be found below:


  1. Set Low to 0 and High to n − 1.
  2. If Low > High, the search terminates as unsuccessful.
  3. Set mid (the position of the middle element) to the floor (the largest previous integer) of (Low + High) / 2.
  4. If arr[mid] < X, set Low to mid + 1 and go to step 2.
  5. If arr[mid] > X, set High to mid – 1 and go to step 2.
  6. Now arr[mid] = X, the search is done; return mid.

Note: The code for the same can be iterative as well as recursive. If you need more knowledge or code for the same, contact me. :)


This summarizes Binary Search . I hope this benefits you!!

Subscribe to the blog for more updates! Good Luck!!

~~Cheers!!




Sunday 4 December 2016

Which all data structures and algorithms does one need to learn for CP?


Below you can find the topics that are required to be done for CP.
  • Binary Search 
  • Quicksort 
  • Merge Sort
  • Suffix Array : 
  • Knuth-Morris-Pratt Algorithm (KMP) 
  • Rabin-Karp Algorithm 
  • Tries 
  • Depth First Traversal of a graph 
  • Breadth First Traversal of a graph
  • Flood Fill
  • Dijkstra's Algorithm 
  • Binary Indexed Tree
  • Segment Tree (with lazy propagation) : 
  • Persistent Segment Tree
  • problems same as BIT,
  • Z algorithm 
  • Floyd Warshall Algorithm 
  • Sparse Table(RMQ)  
  • Heap / Priority Queue / Heapsort
  • Suffix Automaton 
  • Lowest Common Ancestor :
  • Counting Inversions
  • Suffix Tree 
  • Dynamic Programming : chapter from clrs(essential)
  • dp on trees 
  • Basic Data Structures 
  • Graphs 
  • Minimum Spanning Tree 
  • Combinatorics 
  • Union Find/Disjoint Set 
  • Knapsack problem 
  • Aho-Corasick String Matching Algorithm 
  • Strongly Connected Components 
  • Bellman Ford algorithm 
  • heavy-light Decomposition 
  • Convex Hull 
  • Line Intersection 
  • Sieve of Erastothenes
  • Interval Tree 
  • Counting Sort
  • Probabilities
  • Building up the recurrence matrix to compute recurrences in O(logN) time
  • Network flow 
  • K-d tree 
  • Deque
  • Binary Search Tree 
  • Quick Select 
  • Treap/Cartesian Tree
  • Game Theory 
  • STL (C++)
  • Maximum Bipartite Matching
  • Manacher's Algorithm 
  • Miller-Rabin Primality Test
  • Stable Marriage Problem
  • Hungarian Algorithm
  • Sweep line Algorithm 
  • LCP 
  • Gaussian Elimination
  • Pollard Rho Integer Factorization
  • Topological Sorting
  • Detecting Cycles in a Graph : Directed ,Undirected 
  • Geometry 
  • Backtracking : N queens problemTug of WarSudoku
  • Eulerian and Hamiltonian Paths 
  • Graph Coloring 
  • Meet in the Middle
  • Arbitrary Precision Integer(BigInt)
  • Radix SortBucket Sort
  • Johnson's Algorithm 
  • Maximal Matching in a General Graph : Blossom/Edmond's Algorithm with implementationTutte Matrixproblem
  • Recursion
  • Inclusion and Exclusion Principle
This were the topics I could recall right now. Will be updating them if more are there.
In my further blog posts I will be writing about these topics and will be sharing the links to each of the topics mentioned above along with the tutorial and the problems that have been asked in the various contests.

Remember the key to success here is to just practice. You will learn by doing wrong. By that way you can enhance your ability to think.

~~ Cheers!!

Follow the blog for more updates and subscribe it!!


Saturday 3 December 2016

How to get started and from where?

The key to success in Competitive Programming is- "Participate and only Participate". This is the only way you can test your knowledge and ability to think in a limited time frame.By participating you can learn to think of an algorithm in a time frame. There are various online judges that regularly conduct contest as mentioned in the previous answers.
The contests hosted by the judges are really helpful to develop confidence and also these judges have a plethora of questions to practice.
The contests hosted by each of the judge are given below.



  • Codechef -
    1.  Codechef Long Challenge- This is a 10 day long contest consisting of 10 algorithmic questions including one decider question that is an approximate question.
    2. Codechef Cook-Off- CodeChef Cook-Off is a two and half hour coding contest where you can show off your computer programming skills.
    3. Codechef LunchTime- CodeChef Lunchtime is a three hours coding contest where you can show off your computer programming skills.
  • Hackerrank -
    1. Hackerrank Codesprint- They are generally 2 day long algorithmic challenges consisting of 8 problems.
    2. Hackerrank HourRank- This is an hour long challenge consisting of 4 challenges. Fastest coder wins.
  • Codeforces -
    1. Codeforces Division Contest- This is a contest that is hosted by codeforces for generally 4-5 times in a month and the duration is generally 2.5 hours.
Apart from these judges there are various other judges such as Hackerearth etc.
Some big tech Giants also host hackathons that involve algorithms like Google Code Jam , Facebook Hacker Cup and one that targets majorly college students ACM ICPC.


~~Cheers!!

Keep following and subscribe for the blog for more updates!! 






Sunday 20 November 2016

What all languages I should know to be good at Competitive Programming?

The most frequently asked question by the newbies. The answer to this being perfect in any one of the languages of your interest will solve the problems in Competitive Programming. Almost all languages supported by the online judges.

Still recommendation will be any of the one below-

  1. C++
  2. JAVA
  3. C
JAVA is recommended because many times the data you get is out of range of C++ data types(even long long). JAVA has Big Integers that can handle that data. Also many a times PYTHON helps to solve the problem in minimal lines of code. For ex,


This question asks for IRR . PYTHON already has a predefined function for IRR in the module NUMPY. Thus we are left with just 10 lines of code.

PYTHON also can deal with the large Integers but both JAVA and PYTHON have a disadvantage of being slow.

Its not that the large integers questions cannot be solved by C/C++. We need to design some algorithm to handle the same. For ex,


This can be handled by redefining the multiplication algorithm.

~~ Cheers!

Please answer the poll on the right side!!






Saturday 19 November 2016

What all should I know to be a good Competitive Programmer?

Competitive Programming(CP) mainly deals with algorithms. One has to be strong in developing algorithms and should have an excitement to solve a problem.
The most important thing here is the "Patience". You cannot learn Competitive Programming in one day. It has taken years for masters to become masters. But one thing that should keep you motivated is that masters were someday beginners. If they can be why not you?

To develop algorithms, one has to be strong in Mathematics and logical ability to solve a problem. 
The best way to learn in Competitive Programming is to "Practice". And the best way to advance is to fail to solve a problem. You will think of the solution to the problem till the time you are exhausted and that is what is needed. Even you fail many times, don't give up.

A bit of knowledge on Complexity Analysis and Discrete Mathematics is required for a good solver.

There are different types of problems that one can encounter or they could be called a strategy to solve a problem . Those Include-

  1. Dynamic Programming(Knapsack, 8-queens, Traveling Salesman)
  2. Backtracking(8-queens,Sudoku)
  3. Greedy Algorithms (Matching Pursuit, Egyptian Fractions)
  4. Sorting (Quick Sort, Merge-sort)
  5. Search(Linear Search,Binary Search)
  6. Path Finding(BFS,DFS)
  7. Network Flow Problems (Minimum Cut, Max Flow)
  8. Combinatorial  (Permutations, Calendrical Calculations)
  9. Geometrical (Convex Hull, Minkwoski Sum)
Having knowledge about these algorithms is surely a plus as it will help you to think about a problem's solution and once you are through with these, you can easily optimise your solution according to the requirement of the problem.


~~Cheers!

What is Competitive Programming?



Competitive Programming is the sport of programming where in you can learn while competing with programmers round the globe. Many students consider this as a source to get a job, but it is more of a learning journey .
In India, Competitive Programming wasn't in a trend and students don't really know about it. This blog will help you  to get introduced with it and how you can improve your journey to become a good Programmer.

There are various online  judges that can help you get started. Some of the best ones to get started are-

  1. Codechef
  2. Hackerrank
  3. Codeforces
  4. TopCoder
  5. HackerEarth
Among the judges listed above the best tutorials can be found on Hackerearth(CodeMonks) and Topcoder.

To get started with Hackerrank , I personally made a video for the Beginners to get started.


This blog will cover all the topics required to get into Competitive Programming.

~~Cheers!