[LeetCode] Sum of Squares of Special Elements

2778. Sum of Squares of Special Elements

You are given a 1-indexed integer array nums of length n.

An element nums[i] of nums is called special if i divides n, i.e. n % i == 0.

Return the sum of the squares of all special elements of nums.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int sumOfSquares(vector<int>& nums) {
int res = 0, n = nums.size();
for(int i = 1; i <= nums.size(); i++) {
if(n % i) continue;
res += nums[i-1] * nums[i-1];
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/07/16/PS/LeetCode/sum-of-squares-of-special-elements/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.