From 59d42a16e40f32dd401d275c947f0d8428032413 Mon Sep 17 00:00:00 2001 From: aryansoni25 Date: Wed, 19 Aug 2026 21:44:32 +0530 Subject: [PATCH 1/3] Add introduction to Maths in DSA --- lessons/intro-maths.md | 230 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) diff --git a/lessons/intro-maths.md b/lessons/intro-maths.md index 9a2aeb7d..4f5da30a 100644 --- a/lessons/intro-maths.md +++ b/lessons/intro-maths.md @@ -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. \ No newline at end of file From c92570a95b46b3bf35c59576090acac1b38ea80f Mon Sep 17 00:00:00 2001 From: aryansoni25 Date: Thu, 20 Aug 2026 15:03:17 +0530 Subject: [PATCH 2/3] Initisl Commit --- lessons/intro-maths.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lessons/intro-maths.md b/lessons/intro-maths.md index 4f5da30a..16ab1e61 100644 --- a/lessons/intro-maths.md +++ b/lessons/intro-maths.md @@ -235,4 +235,4 @@ These mathematical concepts are useful in many DSA problems. - **Permutations and combinations** → counting and combinatorial problems Understanding these mathematical concepts can make it easier to analyze -problems and design efficient algorithms. \ No newline at end of file +problems and design efficient algorithms . From 960dedf30acaa380558bc94f069fa11dc01430cf Mon Sep 17 00:00:00 2001 From: aryansoni25 Date: Thu, 20 Aug 2026 15:04:22 +0530 Subject: [PATCH 3/3] Initisl Commit --- lessons/intro-maths.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lessons/intro-maths.md b/lessons/intro-maths.md index 16ab1e61..5ecb3f47 100644 --- a/lessons/intro-maths.md +++ b/lessons/intro-maths.md @@ -235,4 +235,4 @@ These mathematical concepts are useful in many DSA problems. - **Permutations and combinations** → counting and combinatorial problems Understanding these mathematical concepts can make it easier to analyze -problems and design efficient algorithms . +problems and design efficient algorithms.