3 September 2026 · Advanced track
Shortest Balanced Stretch
Advanced
Lists
Numbers
Find the shortest run of sensor readings that adds up exactly to a target value.
A conveyor belt sensor logs a small integer adjustment value every second. The value can be positive, negative, or zero. Find the length of the shortest contiguous stretch of readings that adds up to exactly a target amount. Return that length as an integer. If no stretch of readings adds up to the target, return -1.
Constraints
readings is a list of integers and can include negative numbers, positive numbers, or zeroreadings can be emptytarget is an integerIf readings is empty, or no contiguous stretch sums to target, return -1
Function signature
def shortest_balanced_stretch(readings, target):