3381. Maximum Subarray Sum With Length Divisible by K LeetCode Solution

In this guide, you will get 3381. Maximum Subarray Sum With Length Divisible by K LeetCode Solution with the best time and space complexity. The solution to Maximum Subarray Sum With Length Divisible by K problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Subarray Sum With Length Divisible by K solution in C++
  4. Maximum Subarray Sum With Length Divisible by K solution in Java
  5. Maximum Subarray Sum With Length Divisible by K solution in Python
  6. Additional Resources
3381. Maximum Subarray Sum With Length Divisible by K LeetCode Solution image

Problem Statement of Maximum Subarray Sum With Length Divisible by K

You are given an array of integers nums and an integer k.
Return the maximum sum of a subarray of nums, such that the size of the subarray is divisible by k.

Example 1:

Input: nums = [1,2], k = 1
Output: 3
Explanation:
The subarray [1, 2] with sum 3 has length equal to 2 which is divisible by 1.

Example 2:

Input: nums = [-1,-2,-3,-4,-5], k = 4
Output: -10
Explanation:
The maximum sum subarray is [-1, -2, -3, -4] which has length equal to 4 which is divisible by 4.

Example 3:

Input: nums = [-5,1,2,-3,4], k = 2
Output: 4
Explanation:
The maximum sum subarray is [1, 2, -3, 4] which has length equal to 4 which is divisible by 2.

Constraints:

1 <= k <= nums.length <= 2 * 105
-109 <= nums[i] <= 109

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

3381. Maximum Subarray Sum With Length Divisible by K LeetCode Solution in C++

class Solution {
 public:
  long long maxSubarraySum(std::vector<int>& nums, int k) {
    long ans = LONG_MIN;
    long prefix = 0;
    // minPrefix[i % k] := the minimum prefix sum of the first i numbers
    vector<long> minPrefix(k, LONG_MAX / 2);
    minPrefix[k - 1] = 0;

    for (int i = 0; i < nums.size(); ++i) {
      prefix += nums[i];
      ans = max(ans, prefix - minPrefix[i % k]);
      minPrefix[i % k] = min(minPrefix[i % k], prefix);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3381. Maximum Subarray Sum With Length Divisible by K LeetCode Solution in Java

class Solution {
  public long maxSubarraySum(int[] nums, int k) {
    long ans = Long.MIN_VALUE;
    long prefix = 0;
    // minPrefix[i % k] := the minimum prefix sum of the first i numbers
    long[] minPrefix = new long[k];
    Arrays.fill(minPrefix, Long.MAX_VALUE / 2);
    minPrefix[k - 1] = 0;

    for (int i = 0; i < nums.length; ++i) {
      prefix += nums[i];
      ans = Math.max(ans, prefix - minPrefix[i % k]);
      minPrefix[i % k] = Math.min(minPrefix[i % k], prefix);
    }

    return ans;
  }
}
// code provided by PROGIEZ

3381. Maximum Subarray Sum With Length Divisible by K LeetCode Solution in Python

class Solution:
  def maxSubarraySum(self, nums: list[int], k: int) -> int:
    ans = -math.inf
    prefix = 0
    # minPrefix[i % k] := the minimum prefix sum of the first i numbers
    minPrefix = [math.inf] * k
    minPrefix[k - 1] = 0

    for i, num in enumerate(nums):
      prefix += num
      ans = max(ans, prefix - minPrefix[i % k])
      minPrefix[i % k] = min(minPrefix[i % k], prefix)

    return ans
# code by PROGIEZ

Additional Resources

See also  73. Set Matrix Zeroes LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.