lundi 30 mars 2020

Can you help me get through the errors in the below problem?I'm stuck

Below is the problem question.
https://leetcode.com/problems/four-divisors/

Some test cases failed to pass:
Input: [21,21]
Output : 32
Expected Output: 64


Please tell me where I'm going wrong.
Also it would be helpful if you suggest me more optimised way.


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);


                }
               else
                   ans.clear();

         }
        return x;
    }
};

Aucun commentaire:

Enregistrer un commentaire