Topics

Typically, in coding contests, we take size of array from input (variable n) and then have to create an array of that size, i.e. we don’t know the array size during compile time. This creates a need for variable length arrays (VLAs). Usually, we get away by doing:

#include <bits/stdc++.h> 
using namespace std; 
signed main() { 
    int n; 
    cin >> n; 
    int arr[n]; 
}

Above works with gcc compiler, but isn’t standard C++, since GCC allows VLAs (variable length arrays) as an extension. In standard C++, we create static arrays with the square bracket notation, i.e. int arr[20] etc.

For variable length arrays, one can use vectors vector<int> arr(n) or dynamic arrays: int* arr = new int[n];

Using vectors is the popular choice as they take care of dynamic array resizing as well.