Remove the Shortest Block to Restore Divisibility

Quick Overview

Remove the Shortest Block to Restore Divisibility. Remove a contiguous block from the positive integer array nums so that the sum left behind is divisible by p. Removing nothing is permitted; removing the entire array is forbidden.

Remove the Shortest Block to Restore Divisibility

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Remove a contiguous block from the positive integer array `nums` so that the sum left behind is divisible by `p`. Removing nothing is permitted; removing the entire array is forbidden. Return the minimum removed length, or `-1` if no allowed removal works. ```cpp class Solution { public: int minSubarray(vector<int>& nums, int p); }; ``` ### Examples **Example 1:** ```text nums = [2,5,3], p = 4 Output: 1 Explanation: Remove 2, leaving a sum of 8. ``` **Example 2:** ```text nums = [1,2], p = 5 Output: -1 Explanation: The only block with the required remainder is the entire array, which cannot be removed. ``` ### Constraints - `1 <= nums.length <= 10^5`. - `1 <= nums[i] <= 10^9`. - `1 <= p <= 10^9`. ### Clarifying Questions A block is contiguous. If the original sum is divisible by p, return zero even when positive-length removals also work. ```hint Inspect the structure Which removals are permitted, and how would you distinguish a valid shortest removal from the forbidden entire array? ``` ### Reasoning checkpoints - Establish divisibility after each claimed removal and justify the minimum removed length. - Distinguish removing nothing, a proper block, and the forbidden whole-array removal. ### Optional extensions Why does the shortest-removal objective favor the latest rather than earliest matching prefix?

Overview: Remove the Shortest Block to Restore Divisibility. Remove a contiguous block from the positive integer array nums so that the sum left behind is divisible by p. Removing nothing is permitted; removing the entire array is forbidden.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Remove a contiguous block from the positive integer array nums so that the sum left behind is divisible by p. Removing nothing is permitted; removing the entire array is forbidden. Return the minimum removed length, or -1 if no allowed removal works.

class Solution {
public:
    int minSubarray(vector<int>& nums, int p);
};

Examples

Example 1:

nums = [2,5,3], p = 4
Output: 1
Explanation: Remove 2, leaving a sum of 8.

Example 2:

nums = [1,2], p = 5
Output: -1
Explanation: The only block with the required remainder is the entire array, which cannot be removed.

Constraints

  • 1 <= nums.length <= 10^5 .
  • 1 <= nums[i] <= 10^9 .
  • 1 <= p <= 10^9 .

Clarifying Questions Guidance

A block is contiguous. If the original sum is divisible by p, return zero even when positive-length removals also work.

Reasoning checkpoints

  • Establish divisibility after each claimed removal and justify the minimum removed length.
  • Distinguish removing nothing, a proper block, and the forbidden whole-array removal.

Optional extensions

Why does the shortest-removal objective favor the latest rather than earliest matching prefix?