πŸ§ͺFormula List

Basic

Prevent iterator from moving forward in for loop.

char ch;

for(int i = 1; i <= 10; i++) {
    cout << "Iteration " << i << endl;
    cout << "repeat iteration (y/n): ";
    cin >> ch;
    
    if(ch == 'y')
    i--;
}

Sometimes you might want to work with the same value of the iterator. Example: See solution for First Missing Integer

Array

  • Address of index i = base address + index * size of each element

  • When working with a group of people in a circular arrangement(or any similar situation), think of how a queue might be used here.

Getting Mid while Avoids Integer Overflow

mid=low+((highβˆ’low)>>1)mid=low+((high-low)>>1)

πŸ’£ mid = low + (high - low) >> 1 will not work. >>'s precedence is lower, so it needs to be in parentheses.

Math

Problem
Approach

Primality Test

Sieve of Eratosthenes

GCD Euclid's Formula

See Main Article

GCD(a,b)={bwhenΒ a=0awhenΒ b=0GCD(a%b,b)whenΒ a>bGCD(a,b%a)whenΒ b>a\text{GCD}(a, b) = \left\{ \begin{array}{ll} b & \text{when } a = 0 \\ a & \text{when } b = 0 \\ GCD(a \% b, b) & \text{when } a > b \\ GCD(a, b \% a) & \text{when } b > a \\ \end{array} \right.

Fast Exponentiation

See Main Article

xn={xn2Γ—xn2whenΒ xΒ isΒ evenΒ andΒ n>=0xn2Γ—xn2Γ—xwhenΒ xΒ isΒ oddΒ andΒ n>=0xn2Γ—xn2whenΒ xΒ isΒ evenΒ andΒ n<0xn2Γ—xn2Γ—1xwhenΒ xΒ isΒ oddΒ andΒ n<0x^n=\left\{ \begin{array}{ll} x^\frac{n}{2}\times x^\frac{n}{2}&\text{when x is even and } n >= 0 \\ x^\frac{n}{2}\times x^\frac{n}{2} \times x&\text{when x is odd and } n >= 0 \\ x^\frac{n}{2}\times x^\frac{n}{2}&\text{when x is even and } n < 0 \\ x^\frac{n}{2}\times x^\frac{n}{2} \times \frac{1}{x} &\text{when x is odd and } n < 0 \\ \end{array} \right.

Compare Numbers Represented as String

Useful when comparing extremely large numbers that cannot otherwise be stored in any other data type

Removing Leading 0s from a Number in String

Compare double type numbers

Two double-type numbers could be the same but could evaluate as false due to precision error when compared using == for equality.

Another way to say that two double numbers are equal is to say that the distance between them is less than Ο΅\epsilon​, where Ο΅\epsilon is a very small number.​

Check whether a double type contains Integer

If a number is not an integer, for example, 5.5, then its difference with its integer part will be non-zero.

If it is an integer, then the difference is 0.

5.5βˆ’5=0.5β‰ 05.0βˆ’5=05.5-5=0.5 \ne 0\\ 5.0-5=0

nCr^nC_r

Time Complexity: O(min(r,nβˆ’r))O(min(r, n - r))

Property of nCr

nCr=nβˆ’1Cr+nβˆ’1Crβˆ’1^nC_r=^{n-1}C_r+^{n-1}C_{r-1}

nPr^nP_r

Property of nPr

nPr=nβˆ’1Pr+rΓ—nβˆ’1Prβˆ’1^nP_r=^{n-1}P_{r}+r \times ^{n-1}P_{r-1}

Power of two

Bit Manipulation

Check if bit is set

Set a bit

Multiply by 2^k

numΓ—2knum \times 2^k

Check if a number is odd or even

String

Problem
Approach

Palindrome Test

Two Pointer Approach

Anagram Test

  • Count of each character in both the strings will be same.

  • Use map or a lookup table.

Linked List

Edge Cases

Case
Problem
Handling

Linked List has single or no elements

Accessing ptr -> next throws an error

if(!A || !(A -> next)) reutrn A

Get Length of a Linked List

Get nth Node from Front

Here n∈[1,A.length]n\in [1, A.length]​

Get the Last Node of a Linked List

Reverse a Linked List

Get the Middle Element of a Linked List

To get 5 as the middle element in event elements

Initialize fast with head->next

Binary Tree

Iterative Traversal Algorithm

Inorder Traversal

Last updated