Data Structure and Algorithms- MATHS (Quick Revision Series PART-1)

Data Structure and Algorithms- MATHS (Quick Revision Series PART-1)

ยท

9 min read

Maths simply make a person more logical, creative, and intelligence

First things, first, i.e MATHEMATICS

Do you want to be a good Software Engineer? Do you want to get good problem-solving skills?

  • Well! then ๐Ÿ‘‡

giphy.gif

Mathematics provides better problem-solving skills and problem-solving is one of the most important factors in the world of Competitive Programming.

So now, if you don't know what to do, how to do and from where to start then here we are gonna discuss those important problems of maths that will increase your accuracy and improve your problem-solving skills.

1. Finding the Number of Digits in a Number!

So here we have been Given an integral number N. The task is to find the count of digits present in this number.

Let's say:

N = 2021

The number of digits in N here is 4 and,
the digits are: 2, 0, 2, 1.

Now you will say that's an easy task we can iterate through each digit in a number and we can easily find the number for digits in O(n) time. ๐Ÿ˜Ž

And what if I say we can find the number of digits in O(1) time. ๐Ÿฅฑ

Better Solution: A better solution is to use mathematics to solve this problem. The number of digits in a number say N can be easily obtained by using the formula:

number of digits K, in N, is = floor(log10(N) + 1)

Since the above algorithm works in a single operation by using two mathematical operations, finding logarithmic and floor value. Therefore, the time complexity of the solution is O(1).

#include<iostream>
#include <cmath>
using namespace std;
int main()
{
    int N, K;
    cin>>N;
    K= floor(log10(N))+1;
    cout<<K;
}

input: 2021
output: 4

giphy (1).gif

Isn't it awesome! โœจ๐ŸŽ‰

2. Arithmetic and Geometric Progressions

AP and GP are also very frequently used mathematical concepts and it's good to memorize their formulas.

~General Formulas to solve problems related to Arithmetic Progressions, If 'a' is the first term and 'd' is a common difference:

nth term of an AP = a + (n-1)*d.

Arithmetic Mean = (Sum of all terms in the AP / Number of terms in the AP)
                               or, b = (a+c)/2.

Sum of โ€˜nโ€™ terms of an AP =  {n (first term + last term)}/2 
                               or, { n(2a + (n-1)d) }/2.

~General Formulas to solve problems related to Geometric Progressions:

If โ€˜aโ€™ is the first term and โ€˜rโ€™ is the common ratio:

nth term of a GP = (a*r)^n-1.

Geometric Mean = nth root of the product of n terms in the GP
                            or, b = โˆš(a*c).

Sum of โ€˜nโ€™ terms of a GP (r > 1) = ( a(r^n  โ€“1) ) / (rโ€“1).

Sum of infinite terms of a GP (r < 1) = (a) / (1 โ€“ r).

FUN FACT ABOUT GP:

The behavior of a geometric sequence depends on the value of the common ratio. If the common ratio is:

  • Positive, the terms will all be the same sign as the initial term.

  • Negative, the terms will alternate between positive and negative.

  • Greater than 1, there will be exponential growth towards positive or negative infinity (depending on the sign of the initial term), and if 1, then the progression is a constant sequence.

  • Between -1 and 1 but not zero, there will be exponential decay towards zero.

giphy2.gif

3. Quadratic Equations

A quadratic equation is a second-order polynomial equation. The general form of a quadratic equation is given as:

a*x^2 + b*x + c = 0

Where a,b and c are real known values and,
a can't be zero.

A quadratic equation has two roots. The roots of a quadratic equation can be easily obtained using the quadratic formula:

roots = (-b ยฑ โˆš(b2 - 4ac))/2a

There arise three cases as described below while finding the roots of a quadratic equation:

  1. If b^2 < 4ac, then roots are complex (not real).
  2. If b^2 = 4ac, then roots are real and both roots are the same.
  3. If b^2 > 4ac, then roots are real and different.

Getting bored??

giphy (2).gif

Lemme ask you one question! ๐Ÿง

How would you print all the PRIME NUMBERS which are less than 50??

Most probably you will do it by using old HACKS, i.e by using for loop and if statement with (n%2 == 0) condition, with the time complexity of O(N).

And what if I say we can do the same in < O(n) time. ๐Ÿฅฑ

Sieve of Eratosthenes ๐Ÿคจ

Sieves algorithm is the most appropriate way of generating Prime numbers.

Following is the algorithm to find all the prime numbers less than or equal to a given integer 'n' by Eratosthenes' method:

Create a list of consecutive integers from 2 to n: (2, 3, 4, ..., n).

  • Initially, let p equal 2, the first prime number.
  • Starting from p^2, count up in increments of p and mark each of these numbers greater than or equal to p^2 itself in the list. These numbers will be p(p+1), p(p+2), p(p+3), etc..
  • Find the first number greater than p in the list that is not marked. If there was no such number, stop.
  • Otherwise, let p now equal this number (which is the next prime), and repeat from step 3.

Now you got a little bit of idea of working on this algorithm, so let's take out pen and paper and do a dry run by going through the below code...

๐Ÿ†—

#include<iostream>
using namespace std;
void primeBySieve(int n)
{
    int arr[100]={0};
    for(int i=2; i<=n; i++)
    { 
        if(arr[i]==0)
        {
            for(int j=i*i; j<=n; j+=i)
            {
                arr[j]=1;
            }
        }}
    for (int i=2; i<=n; i++)
    {
        if(arr[i]==0)
        {
            cout<<i<<" ";
        }
    } cout<<endl;
}
int main()
{
    int n;
    cin>>n;
    primeBySieve(n);
    return 0;
}

4. LCM and HCF

LCM: LCM stands for Least Common Multiple. The lowest number which is exactly divisible by each of the given numbers is called the least common multiple of those numbers.

To find the LCM of the given numbers, express each number as their prime factorization. The product of the highest power of the prime numbers is the LCM.

Consider 2, 3, 6, 8, then the LCM will be 24.

How? ๐Ÿค”

  • Express each number as their prime factorization, i.e, 2, 3, 6(2,3), 8(2,2,2]).

  • Now, The product of the highest power of the prime numbers is the LCM, Prime numbers are 2, 3 and

  • The highest power of 2 is 3 in the case of 8 and the highest power of 3 is 1. i.e, 2^3 * 3^1 = 24

HCF: The term HCF stands for Highest Common Factor. The largest number that divides two or more numbers is the highest common factor (HCF) for those numbers. HCF is also known as the Greatest Common Divisor (GCD).

To find the HCF of two or more numbers, express each number as their prime factorization. The product of the minimum powers of common prime terms in both of the prime factorization gives the HCF.

Consider 30, 42, 45, then the HCF or GCD will be 3.

How? ๐Ÿคจ

  • Express each number as their prime factorization, i.e, (2,3,5), (2,3,7), (3,3,5)
  • The product of the minimum powers of common prime terms in both of the prime factorization gives the HCF, prime numbers are 2,3,5,7.
  • Now since 3 is common in all and its lowest/minimum power is 1. So 3^1 = 3 is GCD.

Cool! but I have heard about the Euclidean Algorithm which we can code. Where it is, and how to use it??

giphy.gif

Euclidean Algorithm follows:

  • If we subtract the smaller number from the larger (we reduce the larger number), GCD doesn't change. So if we keep subtracting repeatedly the larger of two, we end up with GCD.

  • Now instead of subtraction, if we divide the smaller number, the algorithm stops when the remainder is found to be 0.

And bellow is the recursive function for finding GCD using Euclidean Algorithm:

gcd(a, b)
{
    if (a == 0)
        return b;

    return gcd(b % a, a);
}

5. Factorial.

Factorial: In mathematics, the factorial of a number say N is denoted by N!. The factorial of a number is calculated by finding multiplication of all integers between 1 and N(both inclusive.)

For example 5! = 5 x 4 x 3 x 2 x 1 => 120.

N! = N*(N-1)*(N-2)*....*2*1

Note: As, per convention, 0! = 1.

6. Permutation and Combinations Basics

A permutation is the different arrangements of a given number of elements taken one by one, or some, or all at a time. For example, if we have two elements A and B, then there are two possible arrangements, AB and BA.

Important Properties of Permutation:

  • n P(n) = n(n-1)(n-2)......1 = n!.
  • n P (0) = n! / n! = 1.
  • n P (1) = n.
  • n P (n-1) = n!.
  • (n P r) / (n P r-1) = n - r + 1.

The combination is the different selections of a given number of elements taken one by one, or some, or all at a time. For example, if we have two elements A and B, then there is only one way to 00select two items, we select both of them.

Important Properties of Combination:

  • n C 0 = n C n = 1.
  • n C r = n C n-r.
  • n C r + n C r-1 = n+1 C r.
  • n n-1 C r-1 = (n - r + 1) n C r-1.

7. Modular Arithmetic**

Let us take a look at some of the basic rules and properties that can be applied in Modular Arithmetic(Addition, Subtraction, Multiplication, etc.). Consider numbers a and b operated under modulo M.

  • (a + b) mod M = ((a mod M) + (b mod M)) mod M.
  • (a - b) mod M = ((a mod M) - (b mod M)) mod M.
  • (a b) mod M = ((a mod M) (b mod M)) mod M.

There isn't any formula to calculate: (a / b) mod M

The modular inverse is an integer 'x' such that. a x โ‰ก 1 (mod M)

The value of x should be in {0, 1, 2, ... M-1}, i.e., in the ring of integer modulo M.

The multiplicative inverse of "a modulo M" exists if and only if a and M are relatively prime (i.e., if gcd(a, M) = 1).

If a = 3, M = 11
Then multiplicative inverse of 3 will be: 4

Since (4*3) mod 11 = 1, 4 is modulo inverse of 3

So these were some important mathematical concepts that will definitely help in your CP journey.

YoudidIt.gif

Did you find this article valuable?

Support HARDIK KAUSHIK by becoming a sponsor. Any amount is appreciated!