The Wayback Machine - https://web.archive.org/web/20240925164210/https://www.geeksforgeeks.org/some-useful-c-tricks-for-beginners-in-competitive-programming/
Open In App

Some useful C++ tricks for beginners in Competitive Programming

Last Updated : 06 Aug, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Here are some of the basic C++ tricks that every beginner in Competitive Programming should follow for increased speed. However, competitive programming can only be mastered with time and lots of practice. These tricks will just help you to save a little time during the contests which sometimes matters a lot. 

 

  • Using the auto keyword to declare data types can save a lot of time during programming contests. 

    When a variable is defined as auto, the compiler can be determining the datatype on its own. 
    Example: 

auto a = 100; // a will become 'int'
auto b = 1LL; // b will become 'long long'
auto c = 1.0; // c will become 'double'
auto d = "variable"; // d will become 'const char*'
  • The watch macro is one of the most useful tricks ever. 
#define watch(x) cout << (#x) << " is " << (x) << endl
  • If you’re debugging your code, watch(variable); will print the name of the variable and its value. (It’s possible because it’s build in preprocessing time.)
  • Using typedef’s can save a lot of time of yours which you might spend re-writing the same snippet again and again. 
    Example:
typedef long long ll;
typedef pair w;
typedef vector va;
typedef vector vb;
typedef vector vc;

Use of C++ STL

The C++ STL is a very rich library consisting of useful functions, which are super useful and time-saving during contests. For using these functions you need to include a header file. 

#include <algorithm>

We are going to discuss the most commonly used STL algorithmic functions below:-

// for binary search in containers like vector (let target element=6)
binary_search(v.begin(), v.end(), 6); 
// return 1 or 0 as present or not

// max/min of two numbers
ans = max(a,b);
ans = min(a,b);

// max/min of three numbers
ans = max(a,max(b,c));
ans = min(a, min(b,c));

// swap two numbers
swap(a,b);

// reverse containers like vectors, strings
reverse(v.begin(), v.end());

// rotate containers like vector, strings by n position
rotate(v.begin(), v.begin()+n, v.end());

// sort arrays of size n
sort(arr, arr+n);

// sort containers like vector, strings(based on intro sort)
sort(v.begin(), v.end());


Similar Reads

Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
This is a question asked quite often as in which language should be preferred to be efficient in competitive programming. It is something one should not worry about as what matters is the logic, not the language. Most of the languages are more or less same, but till now the most preferred language is C++, and here are the reasons. Python Simple and
5 min read
Tips and Tricks for Competitive Programmers | Set 1 (For Beginners)
This article is a collection of various tips that would help beginners of Competitive programming to get an insight of things that should or shouldn’t be done. Competitive programming can only be improved by “PRACTICE, PRACTICE AND PRACTICE”. Try to solve as many questions you can solve on sites like practice.geeksforgeeks.org. This will enhance yo
6 min read
Which Java libraries are useful for competitive programming?
Java is one of the most recommended languages in competitive programming (please refer a previous article for more details) Java Collection framework contains lots of containers which are useful for different purposes. In this article, we are going to focus on the most important containers from competitive programming and interview preparation poin
4 min read
Which C++ libraries are useful for competitive programming?
C++ is one of the most recommended languages in competitive programming (please refer our previous article for the reason) C++ STL contains lots of containers which are useful for different purposes. In this article, we are going to focus on the most important containers from competitive programming and interview preparation point of view. vector :
3 min read
Which Python Modules are useful for competitive programming?
In the previous article, we have discussed that C++, Java and Python are three most common languages for competitive programming. In this article, we are going to focus on the most important Python modules from competitive programming and interview preparation point of view. list : Dynamic Sized Array that allows insertions and deletions without ca
3 min read
Mathematics Tricks For Competitive Programming In Python 3
Here are some of the exciting features that Python 3.8 provides for programmers in competitive programming. These new features are related to some math functions and algorithms that are frequently used by competitive programmers. The implementation of these features has the time complexity which is the same in case of a program when implemented fro
3 min read
Logarithm tricks for Competitive Programming
Logarithm: is the inverse function of the exponentiation which means the logarithm value of a given number x is the exponent to another number. [Tex]log_b(x) = y, \to b^{y} = x [/Tex] Below are some tricks using Logarithmic function which can be handy in competitive programming. Checking if a number is a power of two or not:Given an integer N, the
15+ min read
C++ tricks for competitive programming (for C++ 11)
We have discussed some tricks in the below post. In this post, some more tricks are discussed. Writing C/C++ code efficiently in Competitive programming Although, practice is the only way that ensures increased performance in programming contests but having some tricks up your sleeve ensures an upper edge and fast debugging. 1) Checking if the numb
5 min read
Bit Tricks for Competitive Programming
In competitive programming or in general, some problems seem difficult but can be solved very easily with little concepts of bit magic. We have discussed some tricks below in the previous post.Bitwise Hacks for Competitive Programming One-Liner Hacks of Bit Manipulation:One-Liner CodeFunctionx&amp;1 Evaluates to 1 if the number is odd else evaluate
7 min read
Java tricks for competitive programming (for Java 8)
Although the practice is the only way that ensures increased performance in programming contests but having some tricks up your sleeve ensures an upper edge and fast debugging.1) Checking if the number is even or odd without using the % operator:Although this trick is not much better than using a % operator but is sometimes efficient (with large nu
5 min read
Some important shortcuts in Competitive Programming
One of the best ways to save time in competitive programming is by creating snippets. Snippets are reusable pieces of code that can be used easily for program creation or repeated tasks. It not only eases the efforts put into writing any code but also saves time for debugging as you know that is stored from the very begging. In this article, we wil
2 min read
Common mistakes to be avoided in Competitive Programming in C++ | Beginners
Not using of 1LL or 1ll when needed // A program shows problem if we // don't use 1ll or 1LL #include &lt;iostream&gt; using namespace std; int main() { int x = 1000000; int y = 1000000; // This causes overflow even // if z is long long int long long int z = x*y; cout &lt;&lt; z; return 0; } Output: -727379968 You might get some other negative valu
7 min read
Some Tricks to solve problems on Impartial games
How to solve problems that fall in the Finders keepers category in 'Game Theory'? Note- Finders keepers game fall into the category of 'Impartial Games' in 'Game Theory'. What are 'Impartial Games'? Let a game is being played between two players 'A' and 'B'. A game between them is said to be 'Impartial' if both the players have same set of moves. W
4 min read
Python Tricks for Competitive Coding
Python is one such programming language that makes everything easier and straight forward. Anyone who has dabbled in python for Competitive Coding gets somewhat addicted to its many features. Here is a list of some of its cool features that I've found most useful in a competitive coding environment. The most common function of the Counter Package.
5 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 precision in competitions or tackling real-world projec
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 edge. Whether you're a seasoned coder or a newcomer
15+ min read
Top Programming Languages For Competitive Programming
Building an application, running a server, or even implementing a game needs a programming language as the foundation. There are almost more than 700 programming languages which are the most popular ones and this number will increase day by day. But, you don't need to learn all of them. Having a good command of anyone is enough for you to grow your
13 min read
How to read Competitive Programming Questions?
Competitive Programming is considered as a sport in the field of computer science. The culture of this sport is growing day by day and a lot of people are also considering this as a career choice. So, to help the participants with improving their efficiency in this sport, in this post, we have tried to cover all possible things, needed to keep in m
5 min read
Importance of Testing In Competitive Programming
Many times while solving problems, we face issues like time limit exceeds, wrong solution, runtime error, and memory limit exceeds because after designing algorithm we don't test algorithm efficiency, correctness, time complexity and running the algorithm on a large set of inputs for testing purpose. This is where stress testing comes for rescue. S
3 min read
Writing code faster during Competitive Programming in C++
This article focuses on how to implement your solutions and implement them fast while doing competitive programming. Setup Please refer Setting up a C++ Competitive Programming Environment Snippets Snippet is a programming term for a small region of re-usable source Code. A lot of modern text editors like Sublime provide you a functionality to auto
3 min read
Competitive Programming vs Software Development - Where Should I Invest My Time?
Competitive Programming....what comes to your mind first...? most probably you associate the idea of solving the coding questions on GeeksforGeeks, Codechef, Topcoder, Codeforces, Hackerrank, etc. What about Software Development...? Most probably you imagine a person building an application, a website, mobile apps, or a person handling some backend
11 min read
5 Best Languages for Competitive Programming
Needless to say, Competitive Programming is one of the most crucial and popular aspects of a programmer's journey. Though, all the programmers are strongly recommended to participate in such coding challenges to enhance their coding skills and to get various ravishing prizes, rewards, and other career opportunities. Meanwhile, there's a question th
5 min read
Fast I/O for Competitive Programming in Python
In Competitive Programming, it is important to read input as fast as possible to save valuable time. Input/Output in Python can be sometimes time taking in cases when the input is huge or to output any numbers of lines or a huge number of arrays(lists) line after line. Fast Input Normally, the input is taken from STDIN in the form of String using i
3 min read
How to get started with Competitive Programming in JavaScript
Competitive programming is a popular activity among programmers looking to enhance their problem-solving skills and algorithmic thinking. JavaScript is a wonderful language for competitive programming because of how simple it is to use, how powerful its libraries are, and how quickly it can prototype ideas. JavaScript is a great option for programm
5 min read
Why Java Language is Slower Than CPP for Competitive Programming?
Choosing the appropriate language while starting competitive programming is the most important factor. Generally, we choose that language that has short syntax and executes very fast or which is familiar to us, and we know all the working patterns of that particular whether it is Java or C++. Most of the programmers use C++ for competitive programm
5 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 template. This saves time during contests and helps
5 min read
How to Use int as long long int for Competitive Programming?
Most of the time, input constraints in Competitive programming questions are bigger than the limits of int. Therefore, there is a need to use long int or even long long int. Here let us take two cases so that if a naive user writes correct logic still input is not getting accepted can get to know where the rectification is required. Case 1: Big int
3 min read
Java Generics to Code Efficiently in Competitive Programming
Templates are the foundation of generic programming, which involve writing code in a way that is independent of any particular type. These powerful tools can be used for writing our code effectively. Some cool tricks that may be used in Competitive Programming are given as follows: Fast Input/Output: This uses the time advantage of BufferedReader a
9 min read
Setting up Java Competitive Programming Environment
An operating system is required to be installed on your system. here we will be discussing the setup in windows. However, you can choose any operating system. Install JDK (Java Development Kit) JDK, is a program that allows you to write Java code from the comfort of your desktop. It contains a variety of tools that are very useful for creating, run
5 min read
Creation and Utilisation of Custom Snippets for Competitive programming in VScode
Coding down the whole algorithm from scratch during the online contest can cost you plenty of time and may lead to a bad rank. There is no point in coding the same algorithm you have coded hundreds of times earlier. Why not utilize your previously coded implementations? You can do that by creating custom code snippets and summoning them just by a k
2 min read
Article Tags :