2302. Count Subarrays With Score Less Than K LeetCode Solution
In this guide, you will get 2302. Count Subarrays With Score Less Than K LeetCode Solution with the best time and space complexity. The solution to Count Subarrays With Score Less Than K problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count Subarrays With Score Less Than K solution in C++
- Count Subarrays With Score Less Than K solution in Java
- Count Subarrays With Score Less Than K solution in Python
- Additional Resources

Problem Statement of Count Subarrays With Score Less Than K
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 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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2302. Count Subarrays With Score Less Than K LeetCode Solution in C++
class Solution {
public:
long long countSubarrays(vector<int>& nums, long long k) {
long ans = 0;
long sum = 0;
for (int l = 0, r = 0; r < nums.size(); ++r) {
sum += nums[r];
while (sum * (r - l + 1) >= k)
sum -= nums[l++];
ans += r - l + 1;
}
return ans;
}
};
/* code provided by PROGIEZ */
2302. Count Subarrays With Score Less Than K LeetCode Solution in Java
class Solution {
public long countSubarrays(int[] nums, long k) {
long ans = 0;
long sum = 0;
for (int l = 0, r = 0; r < nums.length; ++r) {
sum += nums[r];
while (sum * (r - l + 1) >= k)
sum -= nums[l++];
ans += r - l + 1;
}
return ans;
}
}
// code provided by PROGIEZ
2302. Count Subarrays With Score Less Than K LeetCode Solution in Python
class Solution:
def countSubarrays(self, nums: list[int], k: int) -> int:
ans = 0
summ = 0
l = 0
for r, num in enumerate(nums):
summ += num
while summ * (r - l + 1) >= k:
summ -= nums[l]
l += 1
ans += r - l + 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.