GeeksforGeeks App
Open App
Browser
Continue

# Competitive Programming: Conquering a given problem

Programming is the process of developing and implementing sets of instructions to enable a computer to do a certain task.The programming contests like ACM ICPC, Google CodeJam, and IEEE Extreme etc. are delightful playgrounds for the exploration of intelligence of programmers.
From knowing the contest at first to being at ACM ICPC Amritapuri Regionals, I have learnt a lot and would like to share some tips for the coders to tackle the contest problems.

During a real time contest, teams consisting of three students and one computer are to solve as many of the given problems as possible within 5 hours. The team with the most problems solved wins, where ‘solved’ means producing the right outputs for a set of test inputs (trivial and secret). Though the individual skills of the team members are important, in order to be a top team, it is necessary to make use of synergy within the team.

However, to make full use of a strategy, it is also important that your individual skills are as honed as possible. You do not have to be a genius as practicing can take you quite far. As far as feel, there are three factors crucial for being a good programming team:

1. Knowledge of standard algorithms and the ability to find an appropriate algorithm for every problem in the set
2. Ability to code an algorithm into a working program
3. Having a cooperative strategy with your teammates

What is an Algorithm?

Algorithm is a step-by-step sequence of instructions for the computer to follow.

To be able to do something competitive in programming contests, you need to know a lot of well-known algorithms and ability to identify which algorithms is suitable for a particular problem (if the problem is straightforward), or which combinations or variants of algorithms (if the problem is a bit more complex).

Ability to quickly identify problem types

In all programming contests, there are only three types of problems:

1. I haven’t seen this one before.
2. I have seen this type before, but haven’t or can’t solve it.
3. I have solved this type before.

In programming contests, you will be dealing with a set of problems, rather than a single problem. The ability to quickly identify problems into the above mentioned context classifications (haven’t seen, have seen, have solved) will be one of key factor to do well in programming contests. As far as my knowledge and repository, a problem is generally classified amongst given categories:

1. Mathematics : Prime Number, Big Integer, Permutation, Number Theory, Factorial,  Fibonacci, Sequences, Modulus
2. Dynamic Programming : Longest Common Subsequence,Longest Increasing Subsequence, Edit Distance, 0/1 Knapsack, Coin Change, Matrix Chain Multiplication, Max Interval Sum
3. Graph Traversal :Flood Fill,Floyd Warshal, MST, Max Bipartite Matching, Network Flow, Articulation Point
4. Sorting : Bubble Sort, Quick Sort, Merge Sort, Selection Sort, Radix Sort, Bucket Sort
5. Searching : Complete Search, Brute Force, Binary Search
6. String Processing : String Matching, Pattern Matching

You have identified your problem. You think you know how to solve it. The question that you must ask now is simple: Given the maximum input bound (usually given in problem description), can my algorithm, with the complexity that I can compute, pass the time limit given in the programming contest.

Usually, there are more than one way to solve a problem. However, some of them may be incorrect and some of them is not fast enough. However, the rule of thumb is: Brainstorm many possible algorithms – then pick the stupidest that works!

It is useful to memorize the following ordering for quickly determine which algorithm perform better asymptotically: constant < log n < n < n log n < n^2 < n^3 < 2^n < n!

Coding the Problem

Follow the principle KISS: Keep it Simple and Smart, keeping all versions working and stepwise refinement of code.

After you have coded with the best algorithm, matching time/space complexity and satisfying the test cases (sample test cases are trivial, so never measure code correctness according to them and try tricky cases too), then submit the solution – ‘ACCEPTED’
Identifying a tricky test case to get the opponent down is as important as solving the problem where your mind works more at the boundary conditions.

To summarise, these are some key points:

1. Read all the problems, select the problem in order : easy to tough (if everyone is solving a selected problem, do have a look at it)
2. Sketch the algorithms, complexity, conditions, data structures and tricky details
3. Brainstorm other possible algorithms (if any) – then pick the stupidest that works
4. Do all the Math and select the best algorithm.
5. Code it, as fast as possible, and it must be correct.
6. Try to break the algorithm – look out for boundary test cases.

The main mantra to succeed a problem is to keep calm, manage your time and have a strategic approach, rest your experience and practice plays the part.

Keep Practicing !!

This article is written by Vinay Garg. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.