You are given an integer array nums and an integer k.
You may repeatedly choose any contiguous subarray of nums whose sum is divisible by k and delete it; after each deletion, the remaining elements close the gap.
Return the minimum possible sum of nums after performing any number of such deletions.
Example 1:
Input: nums = [1,1,1], k = 2
Output: 1
Explanation:
nums[0..1] = [1, 1], whose sum is 2 (divisible by 2), leaving [1].Example 2:
Input: nums = [3,1,4,1,5], k = 3
Output: 5
Explanation:
nums[1..3] = [1, 4, 1], whose sum is 6 (divisible by 3), leaving [3, 5].nums[0..0] = [3], whose sum is 3 (divisible by 3), leaving [5].
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1061 <= k <= 105Problem Overview: You are given an array and can delete subarrays whose total sum is divisible by k. The goal is to perform deletions so the remaining array sum is as small as possible. The challenge is identifying which segments to remove while avoiding overlapping choices that reduce the final benefit.
Approach 1: Brute Force Subarray Enumeration (O(n2) time, O(1) space)
Compute the sum of every possible subarray and check whether it is divisible by k. For each valid subarray, simulate removing it and track the remaining sum. This requires nested loops to enumerate (i, j) boundaries and repeated sum calculations. While straightforward, it quickly becomes too slow for large arrays because the number of candidate subarrays grows quadratically. This approach mainly helps build intuition about how divisible subarrays affect the final sum.
Approach 2: Prefix Sum + Hash Map Optimization (O(n) time, O(n) space)
The key observation: if two prefix sums have the same remainder when divided by k, the subarray between them has a sum divisible by k. Maintain a running prefixSum and compute prefixSum % k. Use a hash map to store the earliest occurrence of each remainder. When the same remainder appears again, the segment between indices forms a removable subarray. Track the maximum removable sum or the optimal deletion configuration while scanning once through the array.
This method converts the problem into a remainder-matching problem using a prefix sum pattern. The hash table enables constant-time lookups for previously seen remainders, making it efficient for large inputs. Many interview problems with divisibility constraints follow this same pattern with hash tables and modular arithmetic.
Approach 3: Dynamic Programming with Prefix Tracking (O(n) time, O(n) space)
Dynamic programming can track the best achievable removed sum up to each index. Maintain DP states representing the maximum removable sum ending at or before the current index. When a divisible subarray is detected using prefix remainders, update the DP value by combining the current segment with the best previous configuration. This prevents overlapping deletions and guarantees the optimal result. The DP state transitions rely on the same remainder grouping used in the prefix sum method, tying together ideas from dynamic programming and modular prefix sums.
Recommended for interviews: The prefix sum + hash map approach is the expected solution. Interviewers want to see that you recognize the remainder property of divisible subarrays and reduce the search from O(n2) to O(n). Mentioning the brute force approach first shows you understand the problem space, but implementing the prefix remainder technique demonstrates strong algorithmic pattern recognition.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Subarray Enumeration | O(n^2) | O(1) | Useful for understanding the problem or when input size is very small |
| Prefix Sum + Hash Map | O(n) | O(n) | Best general solution for large arrays and typical interview constraints |
| Dynamic Programming with Prefix Remainders | O(n) | O(n) | When multiple deletion choices must be combined optimally without overlap |
Leetcode 3654 | Minimum Sum After Divisible Sum Deletions Detailed explanation | Contest 463 Q3 • Samrat Bhardwaj • 1,206 views views
Watch 8 more video solutions →Practice Minimum Sum After Divisible Sum Deletions with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor