3712. Sum of Elements With Frequency Divisible by K Easy

@problem@discussion
#Array#Hash Table#Counting



1/**
2 * [3712] Sum of Elements With Frequency Divisible by K
3 *
4 * You are given an integer array nums and an integer k.
5 * Return an integer denoting the sum of all elements in nums whose <span data-keyword="frequency-array">frequency</span> is divisible by k, or 0 if there are no such elements.
6 * Note: An element is included in the sum exactly as many times as it appears in the array if its total frequency is divisible by k.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [1,2,2,3,3,3,3,4], k = 2</span>
11 * Output: <span class="example-io">16</span>
12 * Explanation:
13 * 
14 * 	The number 1 appears once (odd frequency).
15 * 	The number 2 appears twice (even frequency).
16 * 	The number 3 appears four times (even frequency).
17 * 	The number 4 appears once (odd frequency).
18 * 
19 * So, the total sum is 2 + 2 + 3 + 3 + 3 + 3 = 16.
20 * </div>
21 * <strong class="example">Example 2:
22 * <div class="example-block">
23 * Input: <span class="example-io">nums = [1,2,3,4,5], k = 2</span>
24 * Output: <span class="example-io">0</span>
25 * Explanation:
26 * There are no elements that appear an even number of times, so the total sum is 0.
27 * </div>
28 * <strong class="example">Example 3:
29 * <div class="example-block">
30 * Input: <span class="example-io">nums = [4,4,4,1,2,3], k = 3</span>
31 * Output: <span class="example-io">12</span>
32 * Explanation:
33 * 
34 * 	The number 1 appears once.
35 * 	The number 2 appears once.
36 * 	The number 3 appears once.
37 * 	The number 4 appears three times.
38 * 
39 * So, the total sum is 4 + 4 + 4 = 12.
40 * </div>
41 *  
42 * Constraints:
43 * 
44 * 	1 <= nums.length <= 100
45 * 	1 <= nums[i] <= 100
46 * 	1 <= k <= 100
47 * 
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/sum-of-elements-with-frequency-divisible-by-k/
52// discuss: https://leetcode.com/problems/sum-of-elements-with-frequency-divisible-by-k/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56impl Solution {
57    pub fn sum_divisible_by_k(nums: Vec<i32>, k: i32) -> i32 {
58        0
59    }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_3712() {
70    }
71}
72

Back
© 2026 bowen.ge All Rights Reserved.