Skip to content

aayush4vedi/MyCompetitiveCoding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Essential Guide To All That Is Essential.md

+ green?
-red?

Cheat Sheets

Mathematics

  • Number theory- HN

  • Print Divisors //O(sqrt(n))

  • Print Pythaogorean triplets in O(n) | Better one

  • Lucas thm-Wilson thm

  • Prime

    • seive of Eratosthenes
    • Miller-Rabin Primality Testing if X*X = (Y*Y)modN && X != +-YmodN, then N is composite
    • Fermat's Little Theorem - given a prime number P, and any number a (where 0<a<p0), then a^(p−1) = 1modp
  • Euclid's algo

    • GCD

      public int GCD(int a, int b)
      {
        if (b==0) return a;
        return GCD(b,a%b);
      }
      
    • lcm - use !...

      public int LCM(int a, int b)
      {
        return b*a/GCD(a,b);    // as a*b = gcd*lcm
      }
      
    • solve linear Diophantine equations of type ax+by =d, d=GCD(a,b) find x,y:

      extendedEuclid(int A, int B) {
          if(B == 0) {
              d = A;
              x = 1;
              y = 0;
          }
          else {
              extendedEuclid(B, A%B);
              int temp = x;
              x = y;
              y = temp - (A/B)*y;
          }   * Naive: try for all the values of B in [1,M-1] // O(M)
      
    • extendedEuclid: If A & M are coprime, Ax + My =1; then x is the answer. //O(log(Max(A,M)))

    • Fermat's Little Theorem- works only when M is prime: since A^(M-1) = 1(mod M) => A^(-1) = (A^(M-2))(mod M), which is the ans i.e.

      int modInverse(int A,int M)
           {
           return modularExponentiation(A,M-2,M);
           }
           //O(logM)                                                ```
           }         
      O(Log(max(A,B))```
      
    • Modular multiplicative inverse ** For given A, M find B such that (A.B)%M =1 **

    • Maths: * A.B = 1(mod M) * B is in range[1,M-1] {(A.B)%M = (A%M * B%M)%M and B=0 is invalid} * Existence of modular multiplicative inverse only when A and M are coprime i.e. GCD(A,M)=1

    • Methods:

  • Geometry

    • Pick’s Theorem for area of polynomials Area = B/2 + I - 1

          B = number of lattice points on the boundary of the polygon
          I = number of lattice points in the interior of the polygon```
      
      
    • Euler’s Formula for polygonal nets V - E + F = 2;V = number of vertices,E = number of edges,F = number of faces

    • Line sweep technique

    • Area of polynomial

    • line-line intersection: orientation

  • Fractions/complex numbers- store num and denom in pairs

    • adding 2 fractions(make denom same first)
    public int[] addFractions(int[] a, int[] b)
    {
       int denom=LCM(a[1],b[1]);
       int[] c={denom/a[1]*a[0] + denom/b[1]*b[0], denom};
       return c;
    }
    
    • reduce a fraction to its simplest form - when the GCD of the numerator and denominator is equal to 1
    public void reduceFraction(int[] a)
    {
       int b=GCD(a[0],a[1]);
       a[0]/=b;
       a[1]/=b;
    }
    
  • Modular arithmetic

    • (a+-b)%c = (a%c +- b%c)%c
    • (a*/b)%c = ((a%c)*/(b%c))%c
  • Exponantiation

    • BinaryExponentiation //O(logN)
    int binaryExponentiation(int x,int n)
    {
        if(n==0)
            return 1;
        else if(n%2 == 0)        //n is even
            return binaryExponentiation(x*x,n/2);
        else                             //n is odd
            return x*binaryExponentiation(x*x,(n-1)/2);
    }
    
    • modularExponentiation
    int modularExponentiation(int x,int n,int M)
     {
         if(n==0)
             return 1;
         else if(n%2 == 0)        //n is even
             return modularExponentiation((x*x)%M,n/2,M);
         else                             //n is odd
             return (x*modularExponentiation((x*x)%M,(n-1)/2,M))%M;
    
     }
     ***
    
    

Data Structures

GraphEssential :My codes for all graph algo's from hackerearth

    int AdjMat[100][100];

    // Adj Matrix
    //   for each line: |V| entries, 0 or the weight
    /*
    0  10   0   0 100   0
   10   0   7   0   8   0
    0   7   0   9   0   0
    0   0   9   0  20   5
  100   8   0  20   0   0
    0   0   0   5   0   0
    */
    Inputting: like 2-D matrix
    for (int i = 0; i < V; i++)
      for (int j = 0; j < V; j++)
        scanf("%d", &AdjMat[i][j]);

    Outputting:
    printf("Neighbors of vertex 0:\n");
    for (int j = 0; j < V; j++)                                    
      if (AdjMat[0][j])
        printf("Edge 0-%d (weight = %d)\n", j, AdjMat[0][j]);
  1. Adjacency List: vector of vector of pairs or array of linked list
typedef pair<int, int> ii;
typedef vector<ii> vii;
vector<vii> AdjList;
/*
2 2 10 5 100
3 1 10 3 7 5 8
2 2 7 4 9
3 3 9 5 20 6 5
3 1 100 2 8 4 20
1 4 5
*/
scanf("%d", &V);
AdjList.assign(V, vii()); // quick way to initialize AdjList with V entries of vii
for (int i = 0; i < V; i++) {
  scanf("%d", &total_neighbors);
  for (int j = 0; j < total_neighbors; j++) {
    scanf("%d %d", &id, &weight);
    AdjList[i].push_back(ii(id - 1, weight));    // some index adjustment
  }
}

printf("Neighbors of vertex 0:\n");
for (vii::iterator j = AdjList[0].begin(); j != AdjList[0].end(); j++)
  // AdjList[0] contains the required information
  // O(k), where k is the number of neighbors
  printf("Edge 0-%d (weight = %d)\n", j->first, j->second);

3.Edge List

priority_queue< pair<int, ii> > EdgeList;   // one way to store Edge List
scanf("%d", &E);
for (int i = 0; i < E; i++) {
  scanf("%d %d %d", &a, &b, &weight);
  EdgeList.push(make_pair(-weight, ii(a, b))); // trick to reverse sort order
}

// edges sorted by weight (smallest->largest)
for (int i = 0; i < E; i++) {
  pair<int, ii> edge = EdgeList.top(); EdgeList.pop();
  // negate the weight again
  printf("weight: %d (%d-%d)\n", -edge.first, edge.second.first, edge.second.second);
}




* 2.Adjacency matrix

  ```cpp
  int AdjMat[100][100];


  /*
  0  10   0   0 100   0
 10   0   7   0   8   0
  0   7   0   9   0   0
  0   0   9   0  20   5
100   8   0  20   0   0
  0   0   0   5   0   0
  */
  Inputting: like 2-D matrix
  for (int i = 0; i < V; i++)
    for (int j = 0; j < V; j++)
      scanf("%d", &AdjMat[i][j]);

  Outputting:
  printf("Neighbors of vertex 0:\n");
  for (int j = 0; j < V; j++)                                    
    if (AdjMat[0][j])
      printf("Edge 0-%d (weight = %d)\n", j, AdjMat[0][j]);
  ```
*  3. Adjacency List
```cpp
typedef pair<int, int> ii;
typedef vector<ii> vii;
vector<vii> AdjList;
/*
2 2 10 5 100
3 1 10 3 7 5 8
2 2 7 4 9
3 3 9 5 20 6 5
3 1 100 2 8 4 20
1 4 5
*/
scanf("%d", &V);
AdjList.assign(V, vii()); // quick way to initialize AdjList with V entries of vii
for (int i = 0; i < V; i++) {
  scanf("%d", &total_neighbors);
  for (int j = 0; j < total_neighbors; j++) {
    scanf("%d %d", &id, &weight);
    AdjList[i].push_back(ii(id - 1, weight));    // some index adjustment
  }
}

printf("Neighbors of vertex 0:\n");
for (vii::iterator j = AdjList[0].begin(); j != AdjList[0].end(); j++)
  // AdjList[0] contains the required information
  // O(k), where k is the number of neighbors
  printf("Edge 0-%d (weight = %d)\n", j->first, j->second);
  • 4.Edge List
priority_queue< pair<int, ii> > EdgeList;   // one way to store Edge List
scanf("%d", &E);
for (int i = 0; i < E; i++) {
  scanf("%d %d %d", &a, &b, &weight);
  EdgeList.push(make_pair(-weight, ii(a, b))); // trick to reverse sort order
}

// edges sorted by weight (smallest->largest)
for (int i = 0; i < E; i++) {
  pair<int, ii> edge = EdgeList.top(); EdgeList.pop();
  // negate the weight again
  printf("weight: %d (%d-%d)\n", -edge.first, edge.second.first, edge.second.second);
}

vedio link:https://www.youtube.com/watch?v=5pNIul92cj0&list=PLTZbNwgO5eboNKSj5qUbXnmuGQb86PuQf&t=2

Algorithms

Problems:

Richa's Work[InterviewBit Microsoft questions]##

Math

Sorting

Binary Search

Arrays

String

linkedlist

Tree

Graph

Greedy

Dyanamic Programming

Stacks

** Computer Network video link** https://www.youtube.com/channel/UCJjC1hn78yZqTf0vdTC6wAQ/playlists

Bit Manipulation

Graphs

About

I now walk into the wild...

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published