Skip to content

Latest commit

 

History

History
275 lines (200 loc) · 3.69 KB

CP_Cheatsheet.md

File metadata and controls

275 lines (200 loc) · 3.69 KB

🏆 Competitive Programming Cheatsheet

Contest Template

⚡ Fast I/O

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // Solution here

    return 0;
}

Test cases handling

int t;
cin >> t;

while(t--) {

    int n;
    cin >> n;

    // solve this test case

}

Range based loop

for(int x : a) {
    cout << x << " ";
}

Macros

Range

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

// sort(all(v));
// sort(rall(v));
// reverse(all(v));

Swap

for swapping

swap(a, b);

swap header ->

#include <utility>

Sort

Sorting header ->

#include <algorithm>

// Sorting
sort(a.begin(), a.end());
sort(a, a + n);

// Descending:
sort(a.rbegin(), a.rend());
sort(a, a + n, greater<int>());

// min/max
min(a,b);
max(a,b);

Boolen function

bool isEven(int x) {
    return x % 2 == 0;
}

if(isEven(x)) {
    cout << "Even";
}

Binary Search

Standard Template

long long lo = 0, hi = 1e18;

while(lo < hi) {
    long long mid = lo + (hi - lo) / 2;

    if(check(mid))
        hi = mid;
    else
        lo = mid + 1;
}

Sort

Ascending order

sort(a.begin(), a.end());

Descending order

sort(a.rbegin(), a.rend());

Iteration

for (string::iterator i = s.begin(); i < s.end(); i++)
        cout << *i << " ";

or,

for (auto i = s.begin(); i < s.end(); i++)
        cout << *i << " ";

Map

For Frequency the occurance

1 2 2 3 3 3
// O(log n) access
map<int,int> mp;

for(int x : a) {
    mp[x]++;
}
mp[1] = 1
mp[2] = 2
mp[3] = 3

Unordered Map

// O(1) access
unordered_map<int,int> mp;

Set

For storing unique values in sorted order

set<int> s;

s.insert(5);
s.insert(2);
s.insert(5);
s.insert(10);

Output

2 5 10

Unique finding

Boolean array approach

bool seen[26] = {};
int distinct = 0;

for (char c : username)
{
    int index = c - 'a';

    if (!seen[index])
    {
        seen[index] = true;
        distinct++;
    }
}

Set approach

set<char> uniqueCharacters;

for (char c : items)
    uniqueCharacters.insert(c);

Rule 1: Plus (+) vs. Minus (-)

  • Character to Number (Index): SUBTRACT (-)
    • Why? You want to "strip away" the base ASCII value to get a clean 0-based index or numerical value.
    • Example: s[i] - 'a' converts a letter character into an index from 0 to 25.
  • Number (Index) to Character: ADD (+)
    • Why? You want to "shift" a plain number up into the valid ASCII character range.
    • Example: i + 'a' converts an index 0–25 back into a letter character.

Rule 2: '0' vs. 'a' vs. 'A'

  • Use '0' when working with digits ('0' through '9').
  • Use 'a' when working with lowercase letters ('a' through 'z').
  • Use 'A' when working with uppercase letters ('A' through 'Z').

Quick Reference Table

Goal Operation Example Explanation
Char Digit to Int ('5' -> 5) c - '0' '5' - '0' 53 - 48 = 5
Int to Char Digit (5 -> '5') num + '0' 5 + '0' 5 + 48 = 53 (prints '5')
Char Letter to Index ('c' -> 2) c - 'a' 'c' - 'a' 99 - 97 = 2
Index to Char Letter (2 -> 'c') i + 'a' 2 + 'a' 2 + 97 = 99 (prints 'c')

Sum by Recusion

long long summation(long long arr[], int n)
{
    if (n == 0) return 0;

    return arr[n - 1] + summation(arr, n-1);
}