Important Beginner Concepts for DSA C++

Jvtech
4 min readJan 29, 2024

--

This article is the ultimate guide for a beginner in C++ and is written with my own experience in C++. So this article aims to cover some basic and important concepts you should know before starting DSA in C++.

  1. Using #include<bits/stdc++.h>
Fig 1- User of include<bits/stdc++.
Fig 1- Code Demo showing benefit of include<bits/stdc++.h>

In Fig 1 code on the left, we need to include 3 different headers for such a small code. As code size increases this slowly becomes frustrating.

So it is better to use #include<bits/stdc++.h> which covers all headers. It includes every standard library. So with this single header, you save time and you don’t need to add other headers.

For online compilers “#include<bits/stdc++.h>” works always but for your local device you need to set it up. For setting this up you can refer to this video — https://www.youtube.com/watch?v=pyn0TjUnf18

2- Explanation of Different Coding Platform Setup

Fig 2 — Normal Leetcode Setup

So whenever a beginner first tries Leetcode he gets confused. He begins to wonder where the int main() function is. So basically what happens is that the int main() function code is usually hidden in the Leetcode platform . Let's take question — https://leetcode.com/problems/two-sum/ . So in Fig 3, I have shown the background or hidden code that runs.

Fig 3 - Actual Code that runs in the background

The hidden code of int main() creates the object of class Solution and calls the function using that object (Refer Fig 3). This happens for all questions. We just need to complete the function twoSum.

For a better understanding, you can refer to the coding questions of GeeksForGeeks. You just need to expand the Driver Code and then you can see the main() code (see Fig 4). It is clearly shown how int main is declared and would clear all your doubts.

Fig 4- GFG Code

3) Computation In Machines (TIME LIMIT EXCEEDED ERROR)

VERY IMPORTANT — Remember a machine could at maximum perform 10⁸ iterations in 1 second. If iterations increase then the time it takes is greater than 1 second. So remember your maximum time complexity should be under 10⁸.

For Egs - if your time complexity of code is O(N) and the maximum value of N is 10⁸ then it would run because O(10⁸) = 10⁸ iterations can run in 1 second but if N is a little greater than 10⁸, for instance, take N as 10⁹ then the code would not execute and would give Time Limit Exceeded Error. This is because machines are trained such that if a code is taking greater than 10⁸ iterations i.e. takes time greater than 1 second then display Time Limit Exceeded Error. So whenever you are solving any problem it is always important to check the range or constraints of N. Below is the screenshot of the Leetcode constraints.

4- Some basic functions regarding vectors and arrays in C++ :

For below examples :

  • vec is the name of the vector
  • arr is the name of the array
  • n is the size of the array

a) sort- Used for sorting vector and array

Syntax 
- For Array
sort(arr,arr+n) where n is size of array and arr is array name
- For Vector
sort(vec.begin(),vec.end()) where vec is vector name

b) For finding the minimum element and maximum element in the vector

auto min=min_element(vec.begin(),vec.end());
cout<<"Minimum element is - "<<*min;

auto max=max_element(vec.begin(),vec.end());
cout<<"Maximum element is - " <<*max;

c) For finding the sum of all elements in the vector

Syntax 
int sum=accumulate(vec.begin(),vec.end(), initial_sum_value);


int sum=accumulate(vec.begin(),vec.end(), 0);

5) Printing Answers in Modulo

Sometimes in coding questions, we are asked to print the answer in modulo.

Why do we need to print in Modulo?

In many programming languages, integers have a maximum representable value. If the result of a computation exceeds this maximum value, it might lead to overflow issues. By using modulo, you can keep the result within a certain range, preventing overflow.

#define MOD 1e9+7;
int requiredAns= 111111111010010; requiredAns
int ans= requiredAns % MOD;

--

--