Skip to content
Discussion options

You must be logged in to vote

We have two prefix sums at indices i and j where (i % k) == (j % k), then the subarray from i+1 to j has length (j - i) which is divisible by k. This is because:

  • i % k == j % k implies (j - i) % k == 0

Approach:

  • Prefix Sum Technique: Calculate cumulative sums while iterating through the array
  • Modulo Tracking: Use the remainder when dividing indices by k to group positions
  • Minimum Prefix Storage: Maintain the smallest prefix sum for each modulo group
  • Subarray Length Control: Ensure subarray length is divisible by k through modulo arithmetic
  • Optimal Update Strategy: Update answer by comparing current prefix minus minimum prefix in same modulo group

Let's implement this solution in PHP: 3…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Nov 27, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Nov 27, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants