Count Subarrays With Score Less Than K
HardUpdated: Aug 2, 2025
Practice on:
Problem
The score of an array is defined as the product of its sum and its length.
- For example, the score of
[1, 2, 3, 4, 5]is(1 + 2 + 3 + 4 + 5) * 5 = 75.
Given a positive integer array nums and an integer k, return thenumber of non-empty subarrays of nums whose score isstrictly less than k.
A subarray is a contiguous sequence of elements within an array.
Examples
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 <= 10^51 <= nums[i] <= 10^51 <= k <= 10^15
Solution
Method 1 - Using Sliding Window
To solve this problem, we need to find the number of non-empty subarrays whose score (product of sum and length) is strictly less than k. Here's the detailed explanation:
Approach
- Sliding Window Technique:
- We use the sliding window method to efficiently compute the sum and length of subarrays in linear time. This avoids computing the score for all possible subarrays, which would take
O(n^2)time.
- We use the sliding window method to efficiently compute the sum and length of subarrays in linear time. This avoids computing the score for all possible subarrays, which would take
- Two Pointers:
- Maintain two pointers (
landr) to track the current subarray. - Expand the window (
r) to include more elements and add them to the cumulative sum. - If the score (
current sum * (r - l + 1)) exceedsk, incrementl(contracting the window) until the score becomes less thank.
- Maintain two pointers (
- Count Valid Subarrays:
- For every
r, the number of valid subarrays ending atris(r - l + 1)because all subarrays starting fromltorsatisfy the condition.
- For every
Code
Java
class Solution {
public long countSubarrays(int[] nums, long k) {
long ans = 0;
long currSum = 0;
int l = 0; // Left pointer
for (int r = 0; r < nums.length; r++) { // Right pointer
currSum += nums[r];
// While the score of the subarray [l...r] is >= k, contract the window
while (currSum * (r - l + 1) >= k) {
currSum -= nums[l];
l++;
}
// Add the number of valid subarrays ending at r
ans += (r - l + 1);
}
return ans;
}
}
Python
class Solution:
def count_subarrays(self, nums: List[int], k: int) -> int:
n = len(nums)
ans = 0
l = 0 # Left pointer
curr_sum = 0
for r in range(n): # Right pointer
curr_sum += nums[r]
while curr_sum * (r - l + 1) >= k:
curr_sum -= nums[l]
l += 1
ans += (r - l + 1)
return ans
Complexity
- ⏰ Time complexity:
O(n). The left pointer moves at most once for every right pointer. Thus, sliding window approach ensuresO(n)time complexity. - 🧺 Space complexity:
O(1)as no additional space is used beyond variables for the algorithm.