Subarray with sum > 0 and length at least k
Problem
Given an array arr containing only the values -1 and 1, and an integer k, determine whether there exists a subarray of length at least k whose elements sum to a value strictly greater than 0.
Input / Output
- Input: integer array
arr(each element is-1or1), integerk. - Output: boolean —
trueif such a subarray exists, otherwisefalse.
Constraints
1 <= arr.length <= 10^51 <= k <= arr.lengtharr[i]is either-1or1.
Example
arr = [-1, 1, 1, -1], k = 2→true(subarray[1, 1]has length 2 and sum 2 > 0).arr = [-1, -1, -1, 1], k = 2→false(no subarray of length >= 2 sums above 0).
asked …