Solution with explanation(Passed all the test cases)


#1
public class Solution {
    public ArrayList<Integer> allFactors(int A) {
        ArrayList<Integer> factors = new ArrayList<>();
        int cf;
        int factor=1;
        while(factor<=Math.sqrt(A)) {
            if(factor==Math.sqrt(A)) {
                /*
                    cf is the co-factor of factor 
                    e.g. A= 36 if factor = 6 then cf = 36/6 = 6
                    so don't need to add twice the same value
                */
                factors.add(factor);
            } else if(A%factor==0) {
                /*
                    cf is the co-factor of factor 
                    e.g. A= 36 if factor = 3 then cf = 36/3 = 12
                */
                factors.add(factor);
                cf=A/factor; 
                factors.add(cf);
            }
            factor++;
        }
        Collections.sort(factors);
        return factors;
    }
}