Sitemap

Competitive Programming

4 min readNov 14, 2019

--

Press enter or click to view image in full size

This blog is series for CP, you are reading part1, part2, part3.

What is competitive programming?

How to start this sport?

How to become master gradually?

So, first part of this blog is introduction to competitive programming. CP is just a mind game you play to solve any problem and make machine understand the same using any programming language. Basically, it is all about logic and your though process along with Data Structures and Algorithms and on the lateral level managing time complexity by analyzing your algorithm and making as optimized as possible to get AC.

To get started there are various popular CP platforms like hackerrank, hackerearth, Codechef, Codeforces, Leetcode, Topcoder in increasing difficulty of problems.

My favorite platform among these is codechef but if you are just starting CP probably hackerrank and hackerearth is best for practice and learning stuff. I was introduced to CP in my second year of engineering! The most important to become better at CP is practice and never give up nature. It will be very difficult in beginning but trust me green color (AC) gives so much satisfaction:)

Codechef hosts three main contests in a month-Long challenge, Cook-off and Lunchtime. Long challenge has eight problems with difficulty increasing after each question and you have ten days to solve them. Long challenge is a great source for learning new concepts and if you give them sincerely you are bound to learn at least one new thing. Cook-off is two and a half hours long contest and comprises of 5 problems. Lunchtime is codechef’s another short contest and it also comprises of 5 problems and you have three hours to solve them.

Press enter or click to view image in full size
AC #Codechef

Codechef is having 3 main contest per month: Long challenge(10 days), Cookoff and lunchtime. You can learn a lot from the span of long challenge so begineer can directly jump in codechef long challenge. Hackerearth circuit is hardest thing I would say (at-least for me) that is also long span challenge but very hard questions but still recommended for intermediate and above coders.

Now let’s get into some technical things. First thing is language, I prefer CPP because I use it! but recent coders are mostly using python(easy and short to write, no need to declare data types of variables!). On the other hand it takes much more time to run, but that we mostly don’t care during CP. The real competition is between JAVA and CPP. I have started coding in JAVA but ended up coding in CPP as it is efficient while it is short contest and speed is must! No doubt there are many differences in both of them, but some of them I want to highlight that relates to CP. CPP is having pointers while JAVA doesn’t. But still I use JAVA (Just for problems having use of BigInteger)! and best stuff about CPP is #define. I use it the most in my code template to get things shorter to write. For example,

Press enter or click to view image in full size
#define _short_ _long_

So, decide your language accordingly, but if you don’t know any of them and just starting then I recommend Python (After all Python is future!)

Get Rushabh Daxini’s stories in your inbox

Join Medium for free to get updates from this writer.

The second thing is time and space complexity. I have started coding in my second year but didn’t know how to analyze code and count complexities! That I learnt in 3rd year(Don’t do the same mistake!). DAA(Design and Analysis of Algorithm) is must. Mostly you need to give output in 1 to maximum 5 seconds (As per given in constraints of problem).

It obviously depends on the machine, how many loops can be run in 1sec. But generally, 1000000(10e6) loops can run in 1 sec time bound. So if 0≤N≤10e6 is given O(N) is okay for 1sec time and if 0≤N≤10 O(N!) and O(2^N) is also good. In case you don’t know what is O(N),

for(int i=0;i<n;i++) runs N times so it is O(N) (To learn this all from beginning open geeksforgeeks time and space complexity

Now, let me take over verdicts you may get,

AC(Accepted) or correct answer (Satisfaction)

Wrong Answer (WA) (Most hated)

Time Limit Exceed (TLE) (Are yaar, fir se!!)

Run-time Error (Various REs are possible) (WTF is wrong!!)

Compilation Error (Should not get)

Memory Limit Exceed (Rare)

Never give up!

Wrong answer means that the output of your code did not match the correct output for that input (or did not fulfill the conditions in case multiple solutions were possible). This is the most frequently occurring bug that you will face and getting rid of it can be a pain. It hurts more when it is just because of “\n”.

Other important part is debugging! After getting WA there is no way other than debug your code. Where it is failing! Dry run whole code give some random input, print flag values and messages. Find corner cases(Most common reason of getting WA)

On the next two parts of blog, I would be discussing some common data structures and algorithms for beginner and some medium hard problem solution (So that one can understand how to approach solution and get AC)

Edit: Link to the next blog!

--

--