Advertisement
imashutosh51

Pascal's triangle

Jul 22nd, 2022 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. //To print the full pascal traingle TC. O(n^2)
  2. class Solution {
  3. public:
  4.     vector<vector<int>> generate(int n) {
  5.         vector <vector <int>> arr(n);
  6.         for(int i=0;i<n;i++){
  7.             arr[i].resize(i+1);  //making the row of required size
  8.             arr[i][0]=arr[i][i]=1;  //making first and last element always 1
  9.             for(int j=1;j<i;j++){   //taking care of middle elements
  10.                 arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
  11.             }
  12.         }
  13.         return arr;
  14.        
  15.     }
  16. };
  17.  
  18. //To print a row of pascal's traingle
  19. /*
  20. Logic:
  21. If you observe then you will find that the nth row elements are :
  22. nc0,nc1,nc2,nc3,nc4,nc5....ncn(where n is 1 based indexing)
  23. so we can calculate nc0,nc1,nc2...one by one.
  24.  
  25. Improvisation:
  26. NCr = (NC(r-1) * (N - r + 1)) / r where 1 ≤ r ≤ N
  27. so if NC0=1 keep it in prev variable
  28. so NC1=1*(N-1+1) and so on.
  29.  
  30. #include <bits/stdc++.h>
  31. using namespace std;
  32. int main()
  33. {
  34.     int N;cin>>N;
  35.     int prev = 1;   // nC0 = 1
  36.     cout << prev;
  37.  
  38.     for (int i = 1; i <= N; i++) {
  39.         // nCr = (nCr-1 * (n - r + 1))/r
  40.         int curr = (prev * (N - i + 1)) / i;
  41.         cout << ", " << curr;  //printing ith element of row
  42.         prev = curr;
  43.     return 0;
  44. }
  45.  
  46.  
  47. */
  48.  
  49. /*
  50. Same above approach if row needed considering large number as the modulo of 10^9+7
  51. */
  52. ll val=pow(10,9)+7;
  53. ll gcdExtended(ll a, ll b, ll *x, ll *y)
  54. {
  55.     // Base Case
  56.     if (a == 0)
  57.     {
  58.         *x = 0, *y = 1;
  59.         return b;
  60.     }
  61.  
  62.     ll x1, y1; // To store results of recursive call
  63.     ll gcd = gcdExtended(b%a, a, &x1, &y1);
  64.  
  65.     // Update x and y using results of recursive
  66.     // call
  67.     *x = y1 - (b/a) * x1;
  68.     *y = x1;
  69.  
  70.     return gcd;
  71. }
  72. ll modInverse(ll b, ll m)
  73. {
  74.     ll x, y; // used in extended GCD algorithm
  75.     ll g = gcdExtended(b, m, &x, &y);
  76.  
  77.     // Return -1 if b and m are not co-prime
  78.     if (g != 1)
  79.         return -1;
  80.  
  81.     // m is added to handle negative x
  82.     return (x%m + m) % m;
  83. }
  84. ll modDivide(ll a, ll b, ll m){
  85.     a = a % m;
  86.     ll inv = modInverse(b, m);
  87.     return ((inv * a) % m);
  88. }
  89. class Solution{
  90. public:
  91.     vector<ll> nthRowOfPascalTriangle(int n) {
  92.         n--;
  93.         ll prev = 1;
  94.         vector <ll> res;
  95.         res.push_back(1);
  96.     for (int i = 1; i <= n; i++) {
  97.         // nCr = (nCr-1 * (n - r + 1))/r
  98.         ll curr = (prev * (n - i + 1))%val;  //This is NC(r-1)*(n-i+1)
  99.         curr=modDivide(curr,i,val);  //This is curr/i
  100.         res.push_back(curr);
  101.         prev = curr;
  102.     }
  103.     return res;
  104.     }
  105. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement