The most common tools to save time in C/C++ are typedefs and macros. Unfortunately, these features are not available in many other languages like Java.
Here are some examples of our C/C++ code
Shortcuts:
// Shortcuts for "common" data types in contests
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
Infinite:
const int INF = 0x3f3f3f3f;
Setting up values in array:
// initialize DP memoization table with -1 memset(memo, -1, sizeof memo); // to clear array of integers memset(arr, 0, sizeof arr); // Use a vector to creates a dynamic array // and initialize it in one statement. // Creates a vector of size N and values as 0. vector<int> v(N, 0); // OR vi v(N, 0);
The following shortcuts can save time in almost all languages including Java:
// to simplify: if (a) ans = b; else ans = c; ans = a ? b : c; // to simplify: ans = ans + val; and its variants ans += val; // index++; if (index >= n) index = 0; index = (index + 1) % n; // index--; if (index < 0) index = n - 1; index = (index + n - 1) % n; // for rounding to nearest integer int ans = (int)((double)d + 0.5); // min/max shortcut to update max/min ans = min(ans, new_computation); // To return true if a value matches a given // number, else return false // if (val == given_no) return true; // else return false; return (val == given_no)
This article is contributed by Priyam kakati. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Top 10 Algorithms and Data Structures for Competitive Programming
- How to begin with Competitive Programming?
- How to become a master in competitive programming?
- Tips and Tricks for Competitive Programmers | Set 1 (For Beginners)
- Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
- Competitive Programming: Conquering a given problem
- Fast I/O for Competitive Programming
- A Better Way To Approach Competitive Programming
- getchar_unlocked() - faster input in C/C++ for Competitive Programming
- Bitwise Hacks for Competitive Programming
- Fast I/O in Java in Competitive Programming
- Frequency Measuring Techniques for Competitive Programming
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Writing C/C++ code efficiently in Competitive programming
- Input/Output from external file in C/C++, Java and Python for Competitive Programming
- Python Input Methods for Competitive Programming
- Prefix Sum Array - Implementation and Applications in Competitive Programming
- Input/Output from external file in C/C++, Java and Python for Competitive Programming | Set 2
- C++ tricks for competitive programming (for C++ 11)


