General Tips
These tips were originally compiled by megatron10599
I recommend using C++ for competitive programming. You should learn about the C++ STL (Standard Template Library), get familiar with using it. You need not dive deep into the implementation details or semantics of the STL itself for the purpose of using it to implement algorithms.
To get going with the STL I recommend this topcoder tutorial (both parts). For C++ reference (function arguments, names etc….
C++ Libraries
The C++ Standard Template Library (STL) The Standard Template Library, or STL, is a C++ library of container classes, algorithms, and iterators; it provides many of the basic algorithms and data structures of computer science. It is a generalized library and so, its components are parameterized: almost every component in the STL is a template.
As mentioned earlier STL has four components
Containers Iterators Algorithms Functions Containers The containers are objects that store data.
Java Libraries
Data Structures ArrayList: ArrayList is present in the java.util package. The advantage of using ArrayList is, it provides dynamic arrays (whose size can be changed). But, it is slightly slower compared to the normal arrays.
1. Adding elements add(Object): To add an object at the end of the list add(int index, Object): To add an object at a specific index 2. Changing elements set(int index, Object): To change the element at an specified index.
Binary Search
What is binary search? Binary search is a search algorithm which finds the position of a certain element (the target value) in a sorted array. It is an important alogrithm as it has a low time complexity. While the idea of the algorithm is very simple, one must be careful while implementing it as it is very easy to make errors.
The idea of binary search Let us try finding the position of 8 in the given sorted array of 7 integers.
Greedy Algorithm
Greedy algorithm is any algorithm that uses the approach of finding the locally optimal choice at each stage in problem solving, greedy algorithm is mostly used in optimisation problem. Greedy algorithm does not always result in the optimal solution, we shall see such an example in the later part of this blog. Let us take a look at a few standard problems to understand greedy algorithm.
Problem (Fractional Knapsack Problem) You have been blessed with the power to predict the prices of shares of $n$ companies a day later.
Number Theory
GCD and LCM GCD of say n numbers is the greatest number which divides each of the n numbers. LCM of n numbers is the smallest number that can be divided by each of the n numbers. These definitions are well known to most of the readers. It is also quite obvious to see that $$ \begin{align} gcd(a_1, a_2, \ldots, a_n) &= gcd(gcd(a_1, a_2), a_3, \ldots, a_n) \\
gcd(a,b) &= gcd(b,a) \\
Basic Graph Algorithms
A graph is a non-linear data structure, used to represent a set of relationships(edges) among a set of things(vertices). https://www.geeksforgeeks.org/graph-data-structure-and-algorithms/
Since, now it is known what a graph is, next task is to represent a graph. The two most common ways to represent a graph are an adjacency list and an adjacency matrix. https://www.geeksforgeeks.org/graph-and-its-representations/
Once it is clear on what is a graph and how to represent it, the next thing to do is to know how to traverse a graph.