ORIGINAL CODING QUESTION
Shortest positive target window
Return the minimum length of a nonempty contiguous segment whose sum is at least target; return 0 if none qualifies.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n positive values, then target.
Constraints
1 ≤ n ≤ 200; 1 ≤ value ≤ 100; 1 ≤ target ≤ 20,000.
Example 1
Input
6 2 3 1 2 4 3 7
Output
2
The segment 4,3 has length two.
Example 2
Input
3 1 1 1 5
Output
0
No segment reaches 5.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Positive values make the sum monotonic as a boundary advances.
Hint 2
Expand until qualifying, then shrink while still qualifying.
Hint 3
Record the shortest qualifying length before removing the left value.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →