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!!