Two approaches 💥||BRUTE O(N) || OPTIMAL O(sqrt(N))
Intuition\nSolution through two approaches.\n\n# Approach\nIn BRUTE approach we can directly iterate through the entire loop and check the given condition and t
# Intuition\nSolution through two approaches.\n\n# Approach\nIn BRUTE approach we can directly iterate through the entire loop and check the given condition and this will lead to O(N) time complexity.\n\nIn the 2nd approach we can optimize it since we know that if \'i\' is a divisor of \'N\' then \'N/i\' will also be a divisor of \'N\'. This will take O(sqrt(N)) time complexity.\n\n# Complexity\n- Time complexity: BRUTE: **O(N)**\n                        OPTIMAL: **O(sqrt(N))**\n\n- Space complexity: O(1)\n\n# Code\n**Time complexity: O(N)**\n```\nclass Solution {\npublic:\n    int sumOfSquares(vector<int>& nums) {\n        int n = nums.size();\n        int ans = 0;\n        for(int i = 0; i < n; i++){\n            if((n % (i + 1)) == 0) ans = ans + nums[i] * nums[i];\n        }\n        return ans;\n    }\n};\n```\n**Time complexity:  O(sqrt(N))**\n```\nint sumOfSquares(vector<int>& nums) {\n    int n = nums.size();\n    int ans = 0;\n    int sqrtN = sqrt(n);\n\n    for(int i = 1; i <= sqrtN; i++) {\n        if (n % i == 0) {\n            ans += nums[i - 1] * nums[i - 1];\n            if (i != n / i) {\n                ans += nums[n / i - 1] * nums[n / i - 1];\n            }\n        }\n    }\n\n    return ans;\n}\n\n```
['Array', 'Math', 'C++']