๐Ÿ‘ฉโ€๐Ÿ’ป
LB DSA Notes & Homework - Abhyas
  • ๐Ÿ˜‡README
  • ๐ŸงชFormula List
    • Basic
    • Arrays
    • Binary Search
    • Math
    • String
    • Bit Manipulation
    • Linked List
  • ๐Ÿš€Functions
  • โš”๏ธQuestion List
  • โœจ01 Introduction to Programming
    • ๐Ÿ“”Handwritten Notes
    • ๐Ÿ“”Handwritten Homework
  • ๐Ÿ”จ02 Programming Basics I
    • ๐Ÿ“”Handwritten Notes
    • ๐Ÿ“”Handwritten homework
  • โš’๏ธ03 Programming Basics II
    • ๐Ÿ“”Handwritten Notes
    • ๐ŸกHomework
      • 01. Difference between if and switch
      • 02. Which values are allowed in switch statement?
      • 03. Print your name N times
      • 04. Sum of n integers using loop
      • 05. What if condition is missing in a loop?
      • 06. Create a calculator using if, else, while and switch
      • 07. Explain do while loop
      • 08. Pre-increment & post-increment MCQ
      • 09. Break and Continue MCQ
      • 10. Variable Scoping MCQ
      • 11. Binary to Decimal
      • 12. Octal to Binary
  • โญ04 Pattern Programs
    • ๐Ÿ“”Handwritten Notes
    • โ›ณAll solutions
  • ๐Ÿ› ๏ธ05 Programming Basics III
    • ๐Ÿ“”Handwritten Notes
    • ๐ŸกHomework
      • 01 LeetCode: Power of Two
      • 02 Reverse an Integer
      • 03 Number Complement
  • ๐Ÿ—’๏ธ06 Arrays and Functions
    • ๐Ÿ“”Handwriteen Notes
  • โ“07 Array Problems I
    • ๐Ÿ“”Handwritten Notes
    • ๐Ÿง‘โ€๐Ÿ’ปClasswork
      • 01 Reverse an Array
      • 02 Swap Using XOR
      • 03 Find Min and Max in array
      • 04 Swap Alternate
      • 05 Sort 0s, 1s and 2s
      • 06 Check if Array is Palindrome
    • โ›ณDiscord Homework List
      • 01. Linear Search
      • 02. Reverse an Array
      • 03. Find max and min element in an array
      • 04. Swap Alternates in an Array
      • 05 Sort 0s, 1s and 2s
      • 06 Move Negative to One Size
      • 07 Union of two Sorted Arrays
      • 07 Intersection of Two Arrays
      • 08 Cyclically Rotate an Array by One
      • 09 Find duplicates in an array of n + 1 integers
      • 10 Two Sum
      • 11 Triplet Sum in Array
      • 12 Check If an Array is a Palindrome
      • 13 Minimum Swaps and K Together
      • 14 Unique number of occurences
      • 15 Kaden's Alorithm
  • โ‰๏ธ08 Array Problems II
    • ๐Ÿ“”Handwritten Notes
    • โ›ณDiscord Homework List
      • 01 Common elements
      • 02 First Repeating Element
      • 03 Non-Repeating Element
      • 04 Subarrays with equal 0s and 1s
      • 05 Find Subarray with Sum 0
      • 06 Factorial of a Large Number
      • โŒ07 Minimize Platforms Problems - GFG
      • โŒ08 Minimize the Heights II - GFG
      • 09 Majority Element
      • 03 Find First non repeating element in an Array
      • 04 Largest subarray with an equal number of 0s and 1s
      • 05 Subarray with 0 sum
      • 06 Factorial of Large Number
      • 07 Minimize Platforms Problems
      • 08 Minimize the Heights II
      • 09 Majority Element (Mooreโ€™s Voting Algorithm)
      • 10 Find whether an array is a subset of another array
  • ๐Ÿงต14 Character Array and Strings
    • ๐Ÿ“”Handwritten Notes
    • ๐Ÿง‘โ€๐Ÿ’ปClasswork
      • Anagram 1 Sort and Compare
      • Anagram 2 Lookup table
      • Check if Strings are rotation of each other
      • getLen() Implementation
      • getline() with delimiter
      • Minimum number of flips
      • Reverse a Character String
    • ๐ŸกHomework
      • 01. Anagram using map
      • 02. Check if One String is in Another
      • 03. cppref string play
    • โ›ณDiscord Homework
      • 01. Length of a string
      • 02. Reverse a String
      • 03. Check If a String is a Palindrome or not
      • 04. Valid Palindrome
      • 05. Reverse words in a string ii
      • 06. Maximum Occurring Character
      • 07. Remove All Occurrences of a Substring
      • 08. Remove all the adjacent duplicates
      • 09. String Compression
      • 10. Permutation in String
  • โž—15. Basic Mathematics for DSA
    • ๐Ÿ“”Handwritten Notes
    • ๐Ÿง‘โ€๐Ÿ’ปClasswork
      • 01 Function to check if number is prime
      • 02 Euclid's Algorithm for GCD
      • 03 Fast Exponentiation
    • ๐ŸกHomework
      • 01. Primality Test
      • 02 Why check till root N in primality test?
      • 03 Leet Code Count Primes
      • 04 Time Complexity of Sieve of Eratosthenes
      • 05 Segmened Sieve
      • 06 Pigeon Hole Principle
  • ๐Ÿ‘‰16 Pointers I
    • ๐Ÿ“”Handwritten Notes
    • ๐ŸกHomework 16
      • Why Does 0x appear in Hex value?
      • Why does pointer size appear 4 bytes on some system and 8 on others?
  • ๐Ÿ‘‰17 Pointers II
    • ๐Ÿ“”Handwritten Notes
    • ๐ŸกHomework
      • How many level deep can pointer to pointer go?
      • Double pointers and funciton play
  • ๐Ÿ’พ18 Static and Dynamic Allocation
    • ๐Ÿ“”Handwritten Notes
    • ๐Ÿง‘โ€๐Ÿ’ปClasswork
      • 01 Dynamic Allocaiton of variable
      • 02 Dynamic Allocation of 1D Array
      • 03 Dynamic Allocation of 2D Array
      • 04 Create jagged array
    • ๐ŸกHomework
      • Why use void pointer?
      • Macros
      • How to know if inline worked or not?
    • โœจBonus
      • new vs malloc
      • Memory Leaks
  • ๐Ÿ”19 Recursion I
    • ๐Ÿ“”Handwritten Notes
    • ๐ŸกHomework
      • 01 Find max in array using recursion
      • 02 Search an Element using Recursion
      • 03 Binary Search using Recursion
    • โ›ณDiscord Homework
      • Reverse String
      • Power of Four
      • Find the Winner of the Circular Game
      • Different Ways to Add Parentheses
  • ๐Ÿ”20 Recursion II
    • ๐Ÿ“”Handwritten Notes
    • ๐Ÿง‘โ€๐Ÿ’ปClasswork
      • 01 Coin Change
    • ๐ŸกHomework
      • 01 Coin Change
      • 02 Optimal Strategy for a Game
  • ๐Ÿ”21 Recursion III
    • ๐Ÿ“”Handwritten Notes
    • ๐ŸกHomework
      • 01 Powerset
      • 02 Combination in a String of Digits
      • 03 Count even length
  • โž—22 Divide and Conquer - Recursion
    • ๐Ÿ“”Handwritten Notes
    • ๐ŸกHomework
      • Merge Sort
      • In-Place Merge Sort
      • Count Inversions
      • Quick Sort
  • โฎ๏ธ23 Backtracking I
    • ๐Ÿ“”Handwritten Notes
    • ๐ŸกHomework
      • Letter Tile Possibilities
      • Generate Parenthesis
      • Beautiful Arrangement
  • 24 Backtracking II
    • ๐Ÿ“”Handwritten Notes
  • โฎ๏ธ25 Backtracking III
    • ๐Ÿ“”Handwritten Notes
  • โณ26 Time Complexity of Recursive Algorithm & OOPs I
    • ๐Ÿ“”Handwritten Notes
  • ๐Ÿ”—29 Linked List I
    • ๐Ÿ“”Handwritten Notes
    • ๐Ÿ‘ฉโ€๐Ÿ’ปCode
    • ๐ŸกHomework
      • Singly Linked List
      • Doubly Linked List
      • Circular Linked List
  • ๐Ÿ”—30 Linked List II
    • ๐Ÿ“”Handwritten Notes
  • ๐Ÿ”—31 Linked List III
    • ๐Ÿ“”Handwritten Notes
  • ๐Ÿ“š33 Stack I
    • ๐Ÿ“”Handwritten Notes
    • ๐Ÿ‘ฉโ€๐Ÿ’ปCode
Powered by GitBook
On this page
  1. 08 Array Problems II
  2. Discord Homework List

06 Factorial of a Large Number

Previous05 Find Subarray with Sum 0Next07 Minimize Platforms Problems - GFG

Last updated 2 years ago

Question

Given an integer N, find its factorial. Example 1:

Input: N = 5
Output: 120
Explanation : 5! = 1*2*3*4*5 = 120

Example 2:

Input: N = 10
Output: 3628800
Explanation :
10! = 1*2*3*4*5*6*7*8*9*10 = 3628800

Expected Time Complexity: O(N2)O(N^2)O(N2)

Expected Auxilliary Space: O(1)O(1)O(1)โ€‹

Constraints: 1โ‰คNโ‰ค10001 โ‰ค N โ‰ค 10001โ‰คNโ‰ค1000

Using Vector for Large Number

Logic
  1. Factorial of larger numbers are unmanageable by any primitive data types.

  2. Use a vector to represent a number

  3. Perform multiplication just like how we used to do in school by hand

  4. To multiply vector v with integer x

    1. Multiply each digit of v by x

    2. Update carry

    3. Replace each digit of v

Code
void multiply(vector<int> &res, int x) {
    long long product, carry = 0;
    for(int i = 0; i < res.size(); i++) {
        product = res[i] * x + carry;
        res[i] = product % 10;
        carry = product / 10;
    }
    
    while(carry) {
        res.push_back(carry % 10);
        carry /= 10;
    }
}

vector<int> factorial(int N){
    vector<int> res = {1};
    for(int i = 1; i <= N; i++)
        multiply(res, i);
        
    reverse(res.begin(), res.end());
    
    return res;
}
Complexity

Time Complexity: O(n)O(n)O(n)โ€‹

Space Complexity: O(n)O(n)O(n)โ€‹

โ‰๏ธ
โ›ณ
Factorials of large numbers | Practice | GeeksforGeeks
Logo