↓Skip to main content
  1. LeetCode/

LeetCode 2875: Minimum Size Subarray in Infinite Array

·2 mins· ·
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-18

  • Submissions: Wrong Answer ×2 → Accepted

Approach
#

Let total = sum(nums) and full, rest = divmod(target, total).

  • A subarray of length at least n contains a whole cycle (sum total); removing it still leaves a contiguous subarray of the infinite array. So the answer = full × n + the shortest subarray summing to exactly rest.
  • rest < total with positive numbers, so that subarray is shorter than n and spans at most two copies of nums; a sliding window over nums + nums is enough.
  • When rest == 0, the answer is full × n.

My Attempts
#

VersionProblem
Attempt 1Replaced target with target % sum and forgot to add the full cycles back. For nums = [1], target = 6, the answer is 6; it returned 0
Attempt 2Added n * divisor back, but computed sum after doubling nums, so the total was twice too large. For nums = [1, 4, 2], target = 24, the answer is 11; it returned -1
ACComputed the original sum and cycle count first, then doubled nums

Solution
#

class Solution:
    def minSizeSubarray(self, nums: List[int], target: int) -> int:
        n = len(nums)
        full, rest = divmod(target, sum(nums))     # use the original sum
        if rest == 0:
            return full * n

        arr = nums + nums                          # the remainder spans at most two copies
        left = window = 0
        best = float("inf")
        for right, x in enumerate(arr):
            window += x
            while window > rest:
                window -= arr[left]
                left += 1
            if window == rest:
                best = min(best, right - left + 1)
        return -1 if best == float("inf") else full * n + best

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Handling only the remainder and forgetting the full cycles.
  • Doubling the array before summing: the total doubles too.

Interview Explanation
#

A long subarray must contain full cycles, so I divide target by the sum to strip them, each worth n elements. The remainder is less than one cycle and all numbers are positive, so it fits within two copies of nums, where a sliding window finds the shortest stretch summing exactly to the remainder. Time O(n).

Related

LeetCode 1248: Count Number of Nice Subarrays
·3 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 1248 note: “exactly k odd numbers” is not monotonic, so compute “at most k” minus “at most k - 1”; or use prefix sums with a hash map
LeetCode 3413: Maximum Coins From K Consecutive Bags
·3 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 3413 note: the best k bags always have one end on a segment boundary; compute “left end at a segment start” and “right end at a segment end” (the latter by mirroring and reusing the same function)
LeetCode 2831: Find the Longest Equal Subarray
·2 mins
LeetCode Medium Sliding-Window Hash-Table Binary-Search Array
LeetCode 2831 note: list the positions of each value and slide a window over that list; the deletions needed are the span of positions minus the number of copies
LeetCode 3795: Minimum Subarray Length With Distinct Sum At Least K
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 3795 note: a shortest-window slide where each value counts once; track each value’s last position, and subtract the left value only if it was the last occurrence
LeetCode 3694: Distinct Points Reachable After Substring Removal
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table String
LeetCode 3694 note: the endpoint is the total displacement minus the removed segment’s displacement; a length-k window tracks that segment and each endpoint goes into a set
LeetCode 3679: Minimum Discards to Balance Inventory
·3 mins
LeetCode Medium Sliding-Window Hash-Table Simulation Counting Array
LeetCode 3679 note: the rule already decides what must be discarded, so simulate it; the length-w window counts only kept arrivals, and discarded ones must not be subtracted when they leave