forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllFactors.java
More file actions
33 lines (25 loc) · 725 Bytes
/
AllFactors.java
File metadata and controls
33 lines (25 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package Math;
import java.util.ArrayList;
/**
* Author - archit.s
* Date - 26/09/18
* Time - 11:57 AM
*/
public class AllFactors {
//Note: Can also be solved by maintaining 2 arrays, one with adding i to last of list and other adding A/i(if A/i != i) first to list
public ArrayList<Integer> allFactors(int A) {
ArrayList<Integer> ans = new ArrayList<>();
int checkN = (int)Math.sqrt(A);
int count = 0;
for(int i = 1; i<= checkN; i++){
if(A%i == 0){
ans.add(count, i);
if(A/i != i){
ans.add(ans.size()-count, A/i);
}
count++;
}
}
return ans;
}
}