Perfect Rank Private Jobs
Perfect RankPrivate Jobs
💼 Placement & IT Recruitment Prep
#9

Maximum Subarray Sum With Length Divisible by K

Medium
Acceptance: 42.5%•ArrayPrefix SumHash TableDynamic Programming•GoogleAmazonMicrosoftMetaBloombergTCS DigitalInfosys DSE

Problem Statement

You are given an array of integers nums and an integer k.

Return the maximum sum of a contiguous subarray of nums such that the size (length) of the subarray is divisible by k.

Examples

Example 1
Input: nums = [1,2], k = 1
Output: 3
Explanation:

The subarray [1, 2] has a sum of 3 and its length is 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 a sum of -10 and its length is 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 a sum of 4 and its length is 4, which is divisible by 2.

Constraints

  • 1 <= k <= nums.length <= 2 * 10^5
  • -10^9 <= nums[i] <= 10^9

Hints

1
2
3
4
5
6
7
8
9
10
Input (nums array, k):
Student & User Feedback

How was your experience on this page?

Your review & improvement suggestions help us refine tools, mock tests, and explanations — it takes only 10 seconds!

Feedback goes directly to our team at perfectrankofficial@gmail.com
Outstanding / Excellent! 🌟
Back to Top of Private Jobs Portal