3381. Maximum Subarray Sum With Length Divisible by K Medium

@problem@discussion
#Array#Hash Table#Prefix Sum



1/**
2 * [3381] Maximum Subarray Sum With Length Divisible by K
3 *
4 * You are given an array of integers nums and an integer k.
5 * Return the maximum sum of a <span data-keyword="subarray-nonempty">subarray</span> of nums, such that the size of the subarray is divisible by k.
6 *  
7 * <strong class="example">Example 1:
8 * <div class="example-block">
9 * Input: <span class="example-io">nums = [1,2], k = 1</span>
10 * Output: <span class="example-io">3</span>
11 * Explanation:
12 * The subarray [1, 2] with sum 3 has length equal to 2 which is divisible by 1.
13 * </div>
14 * <strong class="example">Example 2:
15 * <div class="example-block">
16 * Input: <span class="example-io">nums = [-1,-2,-3,-4,-5], k = 4</span>
17 * Output: <span class="example-io">-10</span>
18 * Explanation:
19 * The maximum sum subarray is [-1, -2, -3, -4] which has length equal to 4 which is divisible by 4.
20 * </div>
21 * <strong class="example">Example 3:
22 * <div class="example-block">
23 * Input: <span class="example-io">nums = [-5,1,2,-3,4], k = 2</span>
24 * Output: <span class="example-io">4</span>
25 * Explanation:
26 * The maximum sum subarray is [1, 2, -3, 4] which has length equal to 4 which is divisible by 2.
27 * </div>
28 *  
29 * Constraints:
30 * 
31 * 	1 <= k <= nums.length <= 2 * 10^5
32 * 	-10^9 <= nums[i] <= 10^9
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/maximum-subarray-sum-with-length-divisible-by-k/
38// discuss: https://leetcode.com/problems/maximum-subarray-sum-with-length-divisible-by-k/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn max_subarray_sum(nums: Vec<i32>, k: i32) -> i64 {
44        
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_3381() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.