Competitive Programming: Conquering a given problem
Last Updated :
28 Mar, 2016
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:
- Knowledge of standard algorithms and the ability to find an appropriate algorithm for every problem in the set
- Ability to code an algorithm into a working program
- 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:
- I haven’t seen this one before.
- I have seen this type before, but haven’t or can’t solve it.
- 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:
- Mathematics : Prime Number, Big Integer, Permutation, Number Theory, Factorial, Fibonacci, Sequences, Modulus
- Dynamic Programming : Longest Common Subsequence,Longest Increasing Subsequence, Edit Distance, 0/1 Knapsack, Coin Change, Matrix Chain Multiplication, Max Interval Sum
- Graph Traversal :Flood Fill,Floyd Warshal, MST, Max Bipartite Matching, Network Flow, Articulation Point
- Sorting : Bubble Sort, Quick Sort, Merge Sort, Selection Sort, Radix Sort, Bucket Sort
- Searching : Complete Search, Brute Force, Binary Search
- String Processing : String Matching, Pattern Matching
- AdHoc Problems: Trivial Problems
Ability to analyse your algorithm
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:
- 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)
- Sketch the algorithms, complexity, conditions, data structures and tricky details
- Brainstorm other possible algorithms (if any) – then pick the stupidest that works
- Do all the Math and select the best algorithm.
- Code it, as fast as possible, and it must be correct.
- 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.
Similar Reads
Interactive Problems in Competitive Programming
Interactive Problems are those problems in which our solution or code interacts with the judge in real time. When we develop a solution for an Interactive Problem then the input data given to our solution may not be predetermined but is built for that problem specifically. The solution performs a se
6 min read
What are Ad Hoc Problems in Competitive Programming?
The Ad Hoc problems are problems that cannot be classified anywhere else in the categories with well-studied solutions since each problem description and its corresponding solution are unique. These problems don't fall under standard categories, there is no specific or general technique exists to so
5 min read
Learning the art of Competitive Programming
Learning the art of Competitive Programming How to begin with Competitive Programming?Top 10 Algorithms and Data Structures for Competitive ProgrammingHow to prepare for ACM â ICPC?How to prepare for Google Asia Pacific University (APAC) Test ?Remaining Ahead in Competitive Programming:Master in com
2 min read
Competitive Programming vs General Programming
Programming enthusiasts face a crossroads - Competitive Programming or General Programming? This article sheds light on the distinctions between Competitive Programming vs General Programming, offering a quick guide for those navigating the coding landscape. Whether you're aiming for speed and preci
3 min read
Dynamic Programming in Game Theory for Competitive Programming
In the fast-paced world of competitive programming, mastering dynamic programming in game theory is the key to solving complex strategic challenges. This article explores how dynamic programming in game theory can enhance your problem-solving skills and strategic insights, giving you a competitive e
15+ min read
Points to focus on while doing Competitive Programming
Competitive Programming is vital for oneâs development in the coding field. This article is going to discuss some basics points one should keep in mind while competing. Make a list of functions to perform tasks that are encountered frequently in questions and add them to your code in the form of a t
5 min read
Winning and Losing States for Competitive Programming
In Competitive Programming, we often encounter problems where two players are playing a game optimally and we need to find the winner of the game. In order to solve such problems, we should have a clear understanding of Winning and Losing states of the game. In this article, we will go through the b
4 min read
7 Best Books for Competitive Programming
Do you have a dream to win a Gold Medal in the Olympics of Programming (ACM ICPC)? Do you want to ace your career with Google Kickstart or want to win a prize amount of $20,000 to become a world champion in Facebook Hackercup or Google Code jam? Then you have to be an out-of-the-box problem solver.
8 min read
How to begin with Competitive Programming?
At the very beginning to competitive programming, barely anyone knows the coding style to be followed. Below is an example to help you understand how problems are crafted in competitive programming. Let us consider below problem statement as an example. Problem Statement: Linear Search: Given an int
9 min read
Divide and Conquer Optimization in Dynamic Programming
Dynamic programming (DP) is arguably the most important tool in a competitive programmer's repertoire. There are several optimizations in DP that reduce the time complexity of standard DP procedures by a linear factor or more, such as Knuth's optimization, Divide and Conquer optimization, the Convex
15+ min read