Simple C++ solution in O(log(sqrt(n))) & no sorting


#1

vector Solution::allFactors(int A) {
vector factors;
for(int i=1; ii<=A; i++){
//i is a factor;
if(A%i==0)
factors.push_back(i);
}
int n=factors.size();
for(int i=n-1; i>=0; i–){
//avoid repeatation in case of perfect square;
if((factors[i])
(factors[i])!=A)
factors.push_back(A/factors[i]);
}
return factors;
}