Minimum Size Subarray in Infinite Array

You are given a 0-indexed array nums and an integer target.

A 0-indexed array infinite_nums is generated by infinitely appending the elements of nums to itself.

Return the length of the shortest subarray of the array infinite_nums with a sum equal to target. If there is no such subarray, return -1.

Example 1
Inputnums = [1,2,3], target = 5
Output2
The subarray in the range [1,2] has sum 5 and length 2, which is the shortest possible.
Example 2
Inputnums = [1,1,1,2,3], target = 4
Output2
The subarray in the range [4,5] has sum 4 and length 2, which is the shortest possible.

Constraints

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

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate