MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / Solution

Class Solution

LargeFactorial.java:3–38  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1//User function Template for Java
2
3class Solution {
4 static ArrayList<Integer> factorial(int n)
5 {
6 // declare an arrayList
7 ArrayList<Integer> result = new ArrayList<Integer>();
8 int size=0,c=0;
9 // Adding 1 at 0th index
10 result.add(0,1);
11 // Updating size
12 size=1;
13 // Decalre a variable to traverse numbers from 2 to n
14 int val=2;
15 while(val<=n)
16 {
17 // Traverse array list from right to left
18 for(int i=size-1;i>=0;i--)
19 {
20 // Update value in arrayList
21 int temp=result.get(i)*val + c;
22 // Store the last digit at index and add remaining to carry
23 result.set(i,temp%10);
24 // update carry
25 c=temp/10;
26 }
27 // insert carry digit by digit to the begining of the ArrayList
28 while(c!=0)
29 {
30 result.add(0,c%10);
31 c=c/10;
32 size++;
33 }
34 val++;
35 }
36 return result;
37 }
38 }
39

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected