Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 230 additions & 0 deletions lessons/intro-maths.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,233 @@ section: "Maths for DSA"
description: "learn maths required in DSA"
icon: "plus"
---

# Introduction

Mathematics plays an important role in Data Structures and Algorithms.
Many programming problems require mathematical thinking to find an efficient
solution.

Mathematics helps us understand patterns, calculate complexity, work with
numbers, probabilities, and design efficient algorithms.

In this lesson, we will learn some important mathematical concepts that are
useful while studying DSA.

## Maths for DSA

Mathematics is used in DSA to analyze algorithms and solve problems
efficiently.

For example, if an algorithm processes every element of an array once,
the number of operations grows approximately with the size of the array.
This gives us a linear time complexity of `O(n)`.

Mathematical concepts are also useful when working with graphs, recursion,
number-based problems, probability, and counting problems.

## Types of Mathematics Used in DSA

### Discrete Mathematics

Discrete mathematics deals with mathematical structures that are countable
or distinct.

It is useful in DSA because data structures such as graphs, trees, sets,
and relations are based on discrete mathematical concepts.

For example, a set can contain a collection of distinct elements:

```text
A = {1, 2, 3}
B = {2, 3, 4}
```

Graphs are another important application of discrete mathematics. A graph
contains vertices and edges and can be used to represent relationships
between different objects.

Discrete mathematics provides the foundation for understanding many
algorithms involving graphs, trees, sets, and logical operations.

### Recurrence Relations

A recurrence relation defines a problem in terms of smaller instances of
the same problem.

For example, the Fibonacci sequence can be represented as:

```text
F(n) = F(n - 1) + F(n - 2)
F(0) = 0
F(1) = 1
```

Recurrence relations are particularly useful for analyzing recursive
algorithms.

For example, Merge Sort divides an array into two smaller parts and
recursively sorts them. Its recurrence relation can be represented as:

```text
T(n) = 2T(n/2) + O(n)
```

This recurrence leads to a time complexity of `O(n log n)`.

### Number Theory

Number theory deals with properties and relationships between integers.
It is frequently used in programming and competitive programming.

Some important concepts include prime numbers, factors, divisibility,
GCD, LCM, and modular arithmetic.

#### Prime Numbers

A prime number is a positive integer greater than 1 that has exactly two
factors: 1 and itself.

For example:

```text
2, 3, 5, 7, 11, 13
```

are prime numbers.

#### Factors

A factor of a number divides that number without leaving a remainder.

For example, the factors of 12 are:

```text
1, 2, 3, 4, 6, 12
```

#### GCD

The Greatest Common Divisor (GCD) of two numbers is the largest number
that divides both numbers without leaving a remainder.

For example:

```text
GCD(12, 18) = 6
```

The Euclidean algorithm can be used to calculate the GCD efficiently:

```java
static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
```

#### LCM

The Least Common Multiple (LCM) is the smallest positive number that is
divisible by both given numbers.

For example:

```text
LCM(4, 6) = 12
```

For two positive integers, LCM can be calculated using GCD:

```text
LCM(a, b) = |a × b| / GCD(a, b)
```

#### Modular Arithmetic

The modulo operator `%` gives the remainder after division.

For example:

```text
10 % 3 = 1
```

Modular arithmetic is commonly used in algorithms involving large numbers,
cyclic operations, hashing, and competitive programming.

### Probability and Permutations & Combinations

Probability is used to measure how likely an event is to occur.

The basic probability formula is:

```text
Probability = Favorable Outcomes / Total Outcomes
```

For example, when a fair six-sided die is rolled, the probability of
getting a 3 is:

```text
1 / 6
```

Probability can be useful when designing and analyzing randomized
algorithms.

#### Permutations

A permutation is an arrangement of objects where the order matters.

The number of ways to arrange `r` objects from `n` objects is:

```text
P(n, r) = n! / (n - r)!
```

For example, the number of ways to arrange 2 objects from 3 objects is:

```text
P(3, 2) = 3! / (3 - 2)!
= 6
```

#### Combinations

A combination is a selection of objects where the order does not matter.

The number of ways to select `r` objects from `n` objects is:

```text
C(n, r) = n! / (r! × (n - r)!)
```

For example, selecting 2 students from a group of 4 students can be done
in:

```text
C(4, 2) = 6
```

ways.

Permutations and combinations are useful in counting and combinatorial
problems.

## Applications in DSA

These mathematical concepts are useful in many DSA problems.

- **Discrete mathematics** → graphs, trees, sets, and logical operations
- **Recurrence relations** → recursive algorithms and complexity analysis
- **Number theory** → GCD, LCM, prime numbers, factors, and modular arithmetic
- **Probability** → randomized algorithms and probability-based problems
- **Permutations and combinations** → counting and combinatorial problems

Understanding these mathematical concepts can make it easier to analyze
problems and design efficient algorithms.