The moment a beginner hears this word, an image pops up in one’s mind where students are sitting in a room full of Computers and coding some out of the world stuff, We’ll to be honest it’s nothing too hard to grasp so we will be proposing tips that help a programmer to level up faster. So let’s begin with the most underrated points on each topic where even the advanced competitive programmers slip.

So the concept of overflow can be seen in competitive coding a lot, the worst problem of not understanding it is, writing a completely perfect code but Still getting the wrong answer, this is because, at higher input ranges, the compiler strips down the result as per the data size hence it’s important to remember the basic ranges of data types.
Int => [-109 To 109]
Long Int / long => [-1012,1012]
Long Long Int / long long => [-1018,10^18]
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a = 100000, b = 100000;
int c = a * b;
long long int d = a * b;
cout << c << " " << d << endl;
int p = INT_MAX;
cout << "An example of overflow " << p + 1 << endl;
int t, n;
cin >> t;
while (t--) {
cin >> n;
long long pdt = 1, temp;
for ( int i = 0; i < n; i++) {
cin >> temp;
pdt *= temp;
}
if (pdt % 10 == 2 || pdt % 10 == 3 || pdt % 10 == 5)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args)
{
int a = 100000 , b = 100000 ;
int c = a * b;
long d = ( long )a * b;
System.out.println(c + " " + d);
int p = Integer.MAX_VALUE;
System.out.println( "An example of overflow "
+ (p + 1 ));
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0 ) {
int n = sc.nextInt();
long pdt = 1 , temp;
for ( int i = 0 ; i < n; i++) {
temp = sc.nextLong();
pdt *= temp;
}
if (pdt % 10 == 2 || pdt % 10 == 3
|| pdt % 10 == 5 )
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
}
|
C#
using System;
public class MainClass {
public static void Main( string [] args)
{
int a = 100000, b = 100000;
int c = a * b;
long d = ( long )a * b;
Console.WriteLine(c + " " + d);
int p = int .MaxValue;
Console.WriteLine( "An example of overflow "
+ (p + 1));
int t = int .Parse(Console.ReadLine());
while (t-- > 0) {
int n = int .Parse(Console.ReadLine());
long pdt = 1, temp;
for ( int i = 0; i < n; i++) {
temp = long .Parse(Console.ReadLine());
pdt *= temp;
}
if (pdt % 10 == 2 || pdt % 10 == 3
|| pdt % 10 == 5)
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
}
|
Python3
a = 100000
b = 100000
c = a * b
d = a * b
print (c, d)
p = float ( "inf" )
print ( "An example of overflow " , p + 1 )
t = int ( input ())
while t > 0 :
n = int ( input ())
pdt = 1
for i in range (n):
temp = int ( input ())
pdt * = temp
if pdt % 10 = = 2 or pdt % 10 = = 3 or pdt % 10 = = 5 :
print ( "YES" )
else :
print ( "NO" )
t - = 1
|
Output1410065408 1410065408
An example of overflow -2147483648
Tips For Data Structures
Tip 1: In competitive programming always declare the array globally when you need to have a function along with the main function, globally declared arrays can have a size of 10^7 as they are stored in Data Segments.
Tip 2: Whenever you declare a function where you need to return multiple items, or even in general where your goal is to return an array after some modifications, always go for Pass By Reference (eg void swap(int &a, int &b)), it helps you in two ways one, by reducing the time in making a copy of the data and then processing on it and second, as you’re directly working on the main data you can modify as many values as you wish without having to worry about returning the values and dealing with them.
- The limit of the size of an array declared inside the main method is 10^5, coz it’s stored in Stack Memory with a memory limit of around 8MB, if the size is more than this then it results in Stack Overflow.
- A very peculiar error occurs sometimes when you’re taking multiple strings as input after a cin input. instead of taking required inputs, it automatically takes space as input then takes the remaining two strings, to resolve this use cin.ignore() before using the getline() to take the string as input.
- while forming a new string from another string, instead of using syntax like str = str + str2[I], ie adding character to get a string, this method’s time complexity is O(size(string)), Hence listed of adding characters to a string we use push_back(character) whose time complexity is O(n).
Tips on STL
As we already know by far now that STL stands for Standard Template Library in C++, which is a boon for competitive programmers, that contains inbuilt special Data Structures and Algorithm that reduces and optimize your code like Crazy. DO remember following below listed STL data structures tips as follows:
Tip 1: Whenever we need to sort data in a question and remove duplicates, the best practice is to store it in a Set, it automatically sorts the data and as per the property of Set, It only stores unique values.
Tip 2: In questions where we need to find the frequency of all values, the best practice is to use a Map, The property of Map makes it automatically arrange the data in lexicographical order, and just with a little trick, you’ll have the most efficient solution.
The same can be done in java as well, below is the code for above tips.
Example
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< int > v
= { 1, 4, 2, 5, 6, 3, 9, 0, 10, 3, 15, 17, 3 };
auto min = min_element(v.begin(), v.end());
cout << "Min element: " << *min << endl;
auto max = max_element(v.begin(), v.end());
cout << "Max Element: " << *max << endl;
auto sum = accumulate(v.begin(), v.end(), 0);
cout << "The sum of all elements: " << sum << endl;
auto cnt = count(v.begin(), v.end(), 3);
cout << "Frequency Of 3 is: " << cnt << endl;
auto elem = find(v.begin(), v.end(), 3);
if (elem != v.end())
cout << "the element is at posn: " << *elem << endl;
else
cout << "Element not found" << endl;
reverse(v.begin(), v.end());
cout << "Reversed Vector: " ;
for ( auto val : v) {
cout << val << " " ;
}
cout << endl;
sort(v.begin(), v.end());
cout << "Sorted Vector: " ;
for ( auto val : v) {
cout << val << " " ;
}
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>(
Arrays.asList( 1 , 4 , 2 , 5 , 6 , 3 , 9 , 0 , 10 , 3 , 15 , 17 , 3 ));
int min = Collections.min(list);
System.out.println( "Min element: " + min);
int max = Collections.max(list);
System.out.println( "Max Element: " + max);
int sum = 0 ;
for ( int val : list) {
sum += val;
}
System.out.println( "The sum of all elements: " + sum);
int cnt = Collections.frequency(list, 3 );
System.out.println( "Frequency Of 3 is: " + cnt);
int index = list.indexOf( 3 );
if (index != - 1 )
System.out.println( "the element is at posn: " + index);
else
System.out.println( "Element not found" );
Collections.reverse(list);
System.out.print( "Reversed List: " );
for ( int val : list) {
System.out.print(val + " " );
}
System.out.println();
Collections.sort(list);
System.out.print( "Sorted List: " );
for ( int val : list) {
System.out.print(val + " " );
}
}
}
|
Python3
my_list = [ 1 , 4 , 2 , 5 , 6 , 3 , 9 , 0 , 10 , 3 , 15 , 17 , 3 ]
min_element = min (my_list)
print ( "Min element:" , min_element)
max_element = max (my_list)
print ( "Max element:" , max_element)
sum_of_elements = sum (my_list)
print ( "The sum of all elements:" , sum_of_elements)
frequency_of_3 = my_list.count( 3 )
print ( "Frequency of 3 is:" , frequency_of_3)
try :
index = my_list.index( 3 )
print ( "The element is at position:" , index)
except ValueError:
print ( "Element not found" )
my_list.reverse()
print ( "Reversed List:" , my_list)
my_list.sort()
print ( "Sorted List:" , my_list)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass
{
public static void Main( string [] args)
{
List< int > list = new List< int >
{
1, 4, 2, 5, 6, 3, 9, 0, 10, 3, 15, 17, 3
};
int min = list.Min();
Console.WriteLine( "Min element: " + min);
int max = list.Max();
Console.WriteLine( "Max Element: " + max);
int sum = list.Sum();
Console.WriteLine( "The sum of all elements: " + sum);
int cnt = list.Count(x => x == 3);
Console.WriteLine( "Frequency Of 3 is: " + cnt);
int index = list.IndexOf(3);
if (index != -1)
Console.WriteLine( "the element is at posn: " + index);
else
Console.WriteLine( "Element not found" );
list.Reverse();
Console.Write( "Reversed List: " );
foreach ( int val in list)
{
Console.Write(val + " " );
}
Console.WriteLine();
list.Sort();
Console.Write( "Sorted List: " );
foreach ( int val in list)
{
Console.Write(val + " " );
}
}
}
|
Javascript
let myArray = [1, 4, 2, 5, 6, 3, 9, 0, 10, 3, 15, 17, 3];
let minElement = Math.min(...myArray);
console.log( "Min element:" , minElement);
let maxElement = Math.max(...myArray);
console.log( "Max element:" , maxElement);
let sumOfElements = myArray.reduce((a, b) => a + b, 0);
console.log( "The sum of all elements:" , sumOfElements);
let frequencyOf3 = myArray.filter(x => x === 3).length;
console.log( "Frequency of 3 is:" , frequencyOf3);
let index = myArray.indexOf(3);
if (index !== -1) {
console.log( "The element is at position:" , index);
} else {
console.log( "Element not found" );
}
myArray.reverse();
console.log( "Reversed Array:" , myArray);
myArray.sort((a, b) => a - b);
console.log( "Sorted Array:" , myArray);
|
OutputMin element: 0
Max Element: 17
The sum of all elements: 78
Frequency Of 3 is: 3
the element is at posn: 3
Reversed Vector: 3 17 15 3 10 0 9 3 6 5 2 4 1
Sorted Vector: 0 1 2 3 3 3 4 5 6 9 10 15 17
Note: These are some popular algorithms that can make your life easy all have the same pattern of usage ie the name of the function
(lower lim, upper lim+1)