A shop's dashboard shows daily profit, where a negative number is a loss-making day. For every stretch of k consecutive days, the owner wants to see the first loss in that stretch, so they can look up what went wrong first.
Write two functions that take an array of numbers nums and a window size k. The windows are nums[0..k-1], nums[1..k], and so on, so there are nums.length - k + 1 of them.
firstNegatives(nums, k) returns, for each window in order, the first negative value in it, or null if it has none.
firstNegativeIndexes(nums, k) returns, for each window in order, the index (in nums) of that first negative value, or -1 if it has none.
Rules:
"Negative" means < 0. Zero is not negative.
If k is larger than nums.length, there are no windows: return [].
If nums is not an array of finite numbers, throw a TypeError. If k is not a positive integer, throw a RangeError.
The tests use 200,000 values with k up to 100,000. Rescanning every window is O(n × k) and will not finish in time.