2Brute2Best
🔥0

Make Sum Divisible by P

Medium·LeetCode #1590 Pro OnlyTricky / Edge Case
LeetCode
01

Problem Statement

You are given an array nums of positive integers and a number p.

You may delete one contiguous subarray (possibly empty) so that the sum of what is left is divisible by p. You are not allowed to delete the whole array.

Return the length of the shortest subarray you need to delete, or -1 if it cannot be done.

Input

An integer array nums (all values ≥ 1) and an integer p (p ≥ 1).

Output

The smallest length of a subarray to remove (0 if the sum is already divisible), or −1 if only removing everything would work.

Example 1
Input: nums = [3, 1, 4, 2], p = 6
Output: 1

The total is 10. Remove [4] and the rest sums to 6.

Example 2
Input: nums = [6, 3, 5, 2], p = 9
Output: 2

The total is 16. No single element works, but removing [5, 2] leaves [6, 3] with sum 9.

Example 3
Input: nums = [1, 2, 3], p = 3
Output: 0

The total 6 is already divisible by 3, so remove nothing.

Example 4
Input: nums = [1, 2, 3], p = 7
Output: -1

The total is 6. Only removing the whole array would leave a sum divisible by 7, and that is not allowed.

Constraints
  • • 1 ≤ nums.length ≤ 10^5
  • • 1 ≤ nums[i] ≤ 10^9
  • • 1 ≤ p ≤ 10^9
  • • The total can reach 10^14 — use a 64-bit type or keep everything mod p.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“remove a contiguous subarray”
→ The removed piece is a range → difference of two prefix sums.
“rest divisible by p”
→ The piece must have the same remainder as the total: `need = total % p`. Key the map by prefix remainder.
“smallest length”
→ We want the closest matching start → store the **last** index of each remainder.
“not the whole array / return −1”
→ A match of length n does not count; check it at the end.
03

Approach Complexity Ladder

Brute Force — try every piece to removeO(N²)O(1)

Try every subarray; keep the shortest whose sum % p == need

Optimal — last index of each prefix remainderO(N)O(min(N, P))

Look up remainder (cur − need) mod p; store the latest index of each remainder

Brute Force Intuition

Compute need = total % p. For every start, grow the end and keep the piece's remainder. Whenever it equals need, the rest is divisible by p, so record the length. Ignore the whole array.

Recognition rule for this sub-pattern:

Sum divisible by K ⇔ two prefixes have the same remainder. Store remainders, not sums. Fix negative remainders.
Remember this · Prefix Sum
Trigger
Range sums, "subarray sum equals K" (especially with negatives), equal counts, divisible by K.
Think
If I knew the total up to every index, would the answer become a difference of two totals?
Key question
Which earlier prefix value am I looking for when I stand at index i?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.