The score of an array is defined as the product of its sum and its length.
[1, 2, 3, 4, 5]
is (1 + 2 + 3 + 4 + 5) * 5 = 75
.Given a positive integer array nums
and an integer k
, return the number of non-empty subarrays of nums
whose score is strictly less than k
.
A subarray is a contiguous sequence of elements within an array.
Example 1:
Input: nums = [2,1,4,3,5], k = 10 Output: 6 Explanation: The 6 subarrays having scores less than 10 are: - [2] with score 2 * 1 = 2. - [1] with score 1 * 1 = 1. - [4] with score 4 * 1 = 4. - [3] with score 3 * 1 = 3. - [5] with score 5 * 1 = 5. - [2,1] with score (2 + 1) * 2 = 6. Note that subarrays such as [1,4] and [4,3,5] are not considered because their scores are 10 and 36 respectively, while we need scores strictly less than 10.
Example 2:
Input: nums = [1,1,1], k = 5 Output: 5 Explanation: Every subarray except [1,1,1] has a score less than 5. [1,1,1] has a score (1 + 1 + 1) * 3 = 9, which is greater than 5. Thus, there are 5 subarrays having scores less than 5.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 105
1 <= k <= 1015
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to counting qualifying subarrays involves examining every possible subarray to see if it meets the condition. We will methodically generate and evaluate each subarray individually. If it does, we count it and continue until all subarrays are checked.
Here's how the algorithm would work step-by-step:
def count_subarrays_brute_force(numbers, target_score):
subarray_count = 0
# Iterate through all possible starting indices
for start_index in range(len(numbers)):
# Iterate through all possible ending indices for each starting index
for end_index in range(start_index, len(numbers)):
current_subarray = numbers[start_index : end_index + 1]
subarray_sum = 0
# Calculate sum of current subarray
for number in current_subarray:
subarray_sum += number
subarray_score = subarray_sum * len(current_subarray)
# Check if the score of the subarray is less than the target
if subarray_score < target_score:
# Increment the count because it meets the requirement.
subarray_count += 1
return subarray_count
The goal is to count the number of groups of consecutive numbers where the product of their sum and length is less than a target value. Instead of checking every possible group, we use a clever method where we grow and shrink the group based on whether its score is too high or just right.
Here's how the algorithm would work step-by-step:
def count_subarrays_with_score_less_than_k(numbers, target_score):
number_of_subarrays = 0
current_subarray_sum = 0
left_index = 0
for right_index in range(len(numbers)):
current_subarray_sum += numbers[right_index]
# Shrink the subarray from the left if the score is too high.
while (current_subarray_sum * (right_index - left_index + 1)) >= target_score and left_index <= right_index:
current_subarray_sum -= numbers[left_index]
left_index += 1
#If left index exceeds right, no valid subarrays end at right index.
if left_index > right_index:
continue
# Each valid subarray ending at right_index is counted
number_of_subarrays += (right_index - left_index + 1)
return number_of_subarrays
Case | How to Handle |
---|---|
Empty input array (nums is null or has length 0) | Return 0 immediately because there are no subarrays. |
k is zero or negative. | Return 0 because no subarray can have a strictly negative score. |
nums contains negative numbers | The sum of a subarray can be negative; consider how this affects the score (length * sum) compared to k. |
nums contains zero | Zeroes can reduce the sum of subarrays, potentially causing a valid score where it might not have existed otherwise. |
nums contains large positive numbers that could cause integer overflow when summing or when calculating the score (length * sum) | Use long data type to store the sum and score to prevent integer overflow. |
k is a very large number | Ensure the algorithm is efficient enough to avoid unnecessary computations when all subarrays might have a score less than k. |
All elements in nums are the same. | The sum and the score will increase linearly with the length; consider how to efficiently calculate the number of valid subarrays. |
Maximum sized input array (nums is very large) | The algorithm should use O(n) space complexity (sliding window) to avoid exceeding memory limits. |