#K61787. Smallest Positive Subarray Length
Smallest Positive Subarray Length
Smallest Positive Subarray Length
Given an array of integers, your task is to find the length of the smallest contiguous subarray whose sum is greater than zero. A subarray is defined as a contiguous sequence of elements in the array.
If no such subarray exists, output -1
.
For example, consider the array: [1, -2, 3, -1, 2]
. The smallest positive subarray is [3]
, which has a length of 1.
The problem requires an efficient approach that checks subarrays and stops further checking once a positive sum is found for a given starting point.
inputFormat
The input is given from stdin and is formatted as follows:
- The first line contains an integer n representing the number of elements in the array.
- The second line contains n space-separated integers representing the array elements.
outputFormat
Output a single integer to stdout which represents the length of the smallest contiguous subarray with a positive sum. If no such subarray exists, output -1
.
5
1 -2 3 -1 2
1
</p>