Below is the problem question.
https://leetcode.com/problems/four-divisors/
I need a more optimized code.The below code exceeds the time limit.Please suggest me some edits to make this code more optimized.
My solution:
class Solution {
public:
int sumFourDivisors(vector<int>& nums) {
vector<int> ans;
int x=0;
for(int i=0;i<nums.size();i++){
int j=1;
while(j!=nums[i]+1){
if(nums[i]%j==0){
ans.push_back(j);
}
j++;
}
if(ans.size()==4){
x=accumulate(ans.begin(),ans.end(),x);
ans.clear();
}
else
ans.clear();
}
return x;
}
};