A O(n) solution without using sort but using some extra space


#1

vector Solution::allFactors(int A) {
vector ans;
ans.push_back(1);
if(A==1){
return ans;
}
vector big;

for(int i=2;i<=sqrt(A);i++){
if(A%i == 0){
if(A/i ==i){
ans.push_back(i);
continue;
}
else{
ans.push_back(i);
big.push_back(A/i);
}
}
}
for(int i=big.size()-1;i>=0;i–){
ans.push_back(big[i]);
}
ans.push_back(A);
return ans;
}