|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3712.Sum%20of%20Elements%20With%20Frequency%20Divisible%20by%20K/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3712. Sum of Elements With Frequency Divisible by K](https://leetcode.com/problems/sum-of-elements-with-frequency-divisible-by-k) |
| 10 | + |
| 11 | +[中文文档](/solution/3700-3799/3712.Sum%20of%20Elements%20With%20Frequency%20Divisible%20by%20K/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> |
| 18 | + |
| 19 | +<p>Return an integer denoting the <strong>sum</strong> of all elements in <code>nums</code> whose <strong>frequency</strong> is divisible by <code>k</code>, or 0 if there are no such elements.</p> |
| 20 | + |
| 21 | +<p><strong>Note:</strong> An element is included in the sum <strong>exactly</strong> as many times as it appears in the array if its total frequency is divisible by <code>k</code>.</p> |
| 22 | + |
| 23 | +<p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in the array.</p> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | +<p><strong class="example">Example 1:</strong></p> |
| 27 | + |
| 28 | +<div class="example-block"> |
| 29 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,2,3,3,3,3,4], k = 2</span></p> |
| 30 | + |
| 31 | +<p><strong>Output:</strong> <span class="example-io">16</span></p> |
| 32 | + |
| 33 | +<p><strong>Explanation:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li>The number 1 appears once (odd frequency).</li> |
| 37 | + <li>The number 2 appears twice (even frequency).</li> |
| 38 | + <li>The number 3 appears four times (even frequency).</li> |
| 39 | + <li>The number 4 appears once (odd frequency).</li> |
| 40 | +</ul> |
| 41 | + |
| 42 | +<p>So, the total sum is <code>2 + 2 + 3 + 3 + 3 + 3 = 16</code>.</p> |
| 43 | +</div> |
| 44 | + |
| 45 | +<p><strong class="example">Example 2:</strong></p> |
| 46 | + |
| 47 | +<div class="example-block"> |
| 48 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5], k = 2</span></p> |
| 49 | + |
| 50 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 51 | + |
| 52 | +<p><strong>Explanation:</strong></p> |
| 53 | + |
| 54 | +<p>There are no elements that appear an even number of times, so the total sum is 0.</p> |
| 55 | +</div> |
| 56 | + |
| 57 | +<p><strong class="example">Example 3:</strong></p> |
| 58 | + |
| 59 | +<div class="example-block"> |
| 60 | +<p><strong>Input:</strong> <span class="example-io">nums = [4,4,4,1,2,3], k = 3</span></p> |
| 61 | + |
| 62 | +<p><strong>Output:</strong> <span class="example-io">12</span></p> |
| 63 | + |
| 64 | +<p><strong>Explanation:</strong></p> |
| 65 | + |
| 66 | +<ul> |
| 67 | + <li>The number 1 appears once.</li> |
| 68 | + <li>The number 2 appears once.</li> |
| 69 | + <li>The number 3 appears once.</li> |
| 70 | + <li>The number 4 appears three times.</li> |
| 71 | +</ul> |
| 72 | + |
| 73 | +<p>So, the total sum is <code>4 + 4 + 4 = 12</code>.</p> |
| 74 | +</div> |
| 75 | + |
| 76 | +<p> </p> |
| 77 | +<p><strong>Constraints:</strong></p> |
| 78 | + |
| 79 | +<ul> |
| 80 | + <li><code>1 <= nums.length <= 100</code></li> |
| 81 | + <li><code>1 <= nums[i] <= 100</code></li> |
| 82 | + <li><code>1 <= k <= 100</code></li> |
| 83 | +</ul> |
| 84 | + |
| 85 | +<!-- description:end --> |
| 86 | + |
| 87 | +## Solutions |
| 88 | + |
| 89 | +<!-- solution:start --> |
| 90 | + |
| 91 | +### Solution 1: Counting |
| 92 | + |
| 93 | +We use a hash table $\textit{cnt}$ to record the frequency of each number. We traverse the array $\textit{nums}$, and for each number $x$, we increment $\textit{cnt}[x]$ by $1$. |
| 94 | + |
| 95 | +Then, we traverse the hash table $\textit{cnt}$. For each element $x$, if its frequency $\textit{cnt}[x]$ is divisible by $k$, we add $x$ multiplied by its frequency to the result. |
| 96 | + |
| 97 | +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(m)$, where $m$ is the number of distinct elements in the array. |
| 98 | + |
| 99 | +<!-- tabs:start --> |
| 100 | + |
| 101 | +#### Python3 |
| 102 | + |
| 103 | +```python |
| 104 | +class Solution: |
| 105 | + def sumDivisibleByK(self, nums: List[int], k: int) -> int: |
| 106 | + cnt = Counter(nums) |
| 107 | + return sum(x * v for x, v in cnt.items() if v % k == 0) |
| 108 | +``` |
| 109 | + |
| 110 | +#### Java |
| 111 | + |
| 112 | +```java |
| 113 | +class Solution { |
| 114 | + public int sumDivisibleByK(int[] nums, int k) { |
| 115 | + Map<Integer, Integer> cnt = new HashMap<>(); |
| 116 | + for (int x : nums) { |
| 117 | + cnt.merge(x, 1, Integer::sum); |
| 118 | + } |
| 119 | + int ans = 0; |
| 120 | + for (var e : cnt.entrySet()) { |
| 121 | + int x = e.getKey(), v = e.getValue(); |
| 122 | + if (v % k == 0) { |
| 123 | + ans += x * v; |
| 124 | + } |
| 125 | + } |
| 126 | + return ans; |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +#### C++ |
| 132 | + |
| 133 | +```cpp |
| 134 | +class Solution { |
| 135 | +public: |
| 136 | + int sumDivisibleByK(vector<int>& nums, int k) { |
| 137 | + unordered_map<int, int> cnt; |
| 138 | + for (int x : nums) { |
| 139 | + ++cnt[x]; |
| 140 | + } |
| 141 | + int ans = 0; |
| 142 | + for (auto& [x, v] : cnt) { |
| 143 | + if (v % k == 0) { |
| 144 | + ans += x * v; |
| 145 | + } |
| 146 | + } |
| 147 | + return ans; |
| 148 | + } |
| 149 | +}; |
| 150 | +``` |
| 151 | +
|
| 152 | +#### Go |
| 153 | +
|
| 154 | +```go |
| 155 | +func sumDivisibleByK(nums []int, k int) (ans int) { |
| 156 | + cnt := map[int]int{} |
| 157 | + for _, x := range nums { |
| 158 | + cnt[x]++ |
| 159 | + } |
| 160 | + for x, v := range cnt { |
| 161 | + if v%k == 0 { |
| 162 | + ans += x * v |
| 163 | + } |
| 164 | + } |
| 165 | + return |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +#### TypeScript |
| 170 | + |
| 171 | +```ts |
| 172 | +function sumDivisibleByK(nums: number[], k: number): number { |
| 173 | + const cnt = new Map(); |
| 174 | + for (const x of nums) { |
| 175 | + cnt.set(x, (cnt.get(x) || 0) + 1); |
| 176 | + } |
| 177 | + let ans = 0; |
| 178 | + for (const [x, v] of cnt.entries()) { |
| 179 | + if (v % k === 0) { |
| 180 | + ans += x * v; |
| 181 | + } |
| 182 | + } |
| 183 | + return ans; |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +<!-- tabs:end --> |
| 188 | + |
| 189 | +<!-- solution:end --> |
| 190 | + |
| 191 | +<!-- problem:end --> |
0 commit comments