|
| 1 | +# [2778. Sum of Squares of Special Elements](https://leetcode.com/problems/sum-of-squares-of-special-elements) |
| 2 | + |
| 3 | +[中文文档](/solution/2700-2799/2778.Sum%20of%20Squares%20of%20Special%20Elements/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>You are given a <strong>1-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p> |
| 8 | + |
| 9 | +<p>An element <code>nums[i]</code> of <code>nums</code> is called <strong>special</strong> if <code>i</code> divides <code>n</code>, i.e. <code>n % i == 0</code>.</p> |
| 10 | + |
| 11 | +<p>Return <em>the <strong>sum of the squares</strong> of all <strong>special</strong> elements of </em><code>nums</code>.</p> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | +<p><strong class="example">Example 1:</strong></p> |
| 15 | + |
| 16 | +<pre> |
| 17 | +<strong>Input:</strong> nums = [1,2,3,4] |
| 18 | +<strong>Output:</strong> 21 |
| 19 | +<strong>Explanation:</strong> There are exactly 3 special elements in nums: nums[1] since 1 divides 4, nums[2] since 2 divides 4, and nums[4] since 4 divides 4. |
| 20 | +Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[4] * nums[4] = 1 * 1 + 2 * 2 + 4 * 4 = 21. |
| 21 | +</pre> |
| 22 | + |
| 23 | +<p><strong class="example">Example 2:</strong></p> |
| 24 | + |
| 25 | +<pre> |
| 26 | +<strong>Input:</strong> nums = [2,7,1,19,18,3] |
| 27 | +<strong>Output:</strong> 63 |
| 28 | +<strong>Explanation:</strong> There are exactly 4 special elements in nums: nums[1] since 1 divides 6, nums[2] since 2 divides 6, nums[3] since 3 divides 6, and nums[6] since 6 divides 6. |
| 29 | +Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[3] * nums[3] + nums[6] * nums[6] = 2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong>Constraints:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li><code>1 <= nums.length == n <= 50</code></li> |
| 37 | + <li><code>1 <= nums[i] <= 50</code></li> |
| 38 | +</ul> |
| 39 | + |
| 40 | +## Solutions |
| 41 | + |
| 42 | +<!-- tabs:start --> |
| 43 | + |
| 44 | +### **Python3** |
| 45 | + |
| 46 | +```python |
| 47 | +class Solution: |
| 48 | + def sumOfSquares(self, nums: List[int]) -> int: |
| 49 | + n = len(nums) |
| 50 | + return sum(x * x for i, x in enumerate(nums, 1) if n % i == 0) |
| 51 | +``` |
| 52 | + |
| 53 | +### **Java** |
| 54 | + |
| 55 | +```java |
| 56 | +class Solution { |
| 57 | + public int sumOfSquares(int[] nums) { |
| 58 | + int n = nums.length; |
| 59 | + int ans = 0; |
| 60 | + for (int i = 1; i <= n; ++i) { |
| 61 | + if (n % i == 0) { |
| 62 | + ans += nums[i - 1] * nums[i - 1]; |
| 63 | + } |
| 64 | + } |
| 65 | + return ans; |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### **C++** |
| 71 | + |
| 72 | +```cpp |
| 73 | +class Solution { |
| 74 | +public: |
| 75 | + int sumOfSquares(vector<int>& nums) { |
| 76 | + int n = nums.size(); |
| 77 | + int ans = 0; |
| 78 | + for (int i = 1; i <= n; ++i) { |
| 79 | + if (n % i == 0) { |
| 80 | + ans += nums[i - 1] * nums[i - 1]; |
| 81 | + } |
| 82 | + } |
| 83 | + return ans; |
| 84 | + } |
| 85 | +}; |
| 86 | +``` |
| 87 | +
|
| 88 | +### **Go** |
| 89 | +
|
| 90 | +```go |
| 91 | +func sumOfSquares(nums []int) (ans int) { |
| 92 | + n := len(nums) |
| 93 | + for i, x := range nums { |
| 94 | + if n%(i+1) == 0 { |
| 95 | + ans += x * x |
| 96 | + } |
| 97 | + } |
| 98 | + return |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### **TypeScript** |
| 103 | + |
| 104 | +```ts |
| 105 | +function sumOfSquares(nums: number[]): number { |
| 106 | + const n = nums.length; |
| 107 | + let ans = 0; |
| 108 | + for (let i = 0; i < n; ++i) { |
| 109 | + if (n % (i + 1) === 0) { |
| 110 | + ans += nums[i] * nums[i]; |
| 111 | + } |
| 112 | + } |
| 113 | + return ans; |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### **...** |
| 118 | + |
| 119 | +``` |
| 120 | +
|
| 121 | +``` |
| 122 | + |
| 123 | +<!-- tabs:end --> |
0 commit comments