Skip to content

Count Subarrays With Score Less Than K

Problem statement

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 test cases

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

Observation

If there is an array with length \(L\) and \(score < K\), then it produces \(S = 1 + 2 + 3 + ... + L\) different subarrays.

  1. Create the prefix sum array [6-10];
  2. For each \(i_{th}\) find the longest subarray with \(score < K\) [15-31];
  3. Calculate and add \(S\) to the result [33].
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
    long long countSubarrays(vector<int>& nums, long long k) 
    {
        const auto N = nums.size();
        vector<long long> prefix(N, 0);
        prefix[0] = nums[0];

        for(int i = 1; i < N; ++i)
            prefix[i] += nums[i] + prefix[i-1];

        long long res = 0;
        for(int s = 0; s < N; ++s)
        {
            int l = s;
            int r = N;

            while(l < r)
            {
                int m = l + (r - l)/2;
                long long sum = prefix[m] - (s > 0 ? prefix[s-1] : 0);
                long long v =  sum * (m - s + 1);

                if(v < k)
                {
                    l = m + 1;
                }
                else{
                    r = m;
                }
            }

            res += l - s;
        }

        return res;
    }
};

Solution time complexity: \(O(n*log(n))\)
Solution memory complexity: \(O(n)\)

Solution: sliding window

  1. On each iteration extend the sliding window to the right and calculate the new sum;
  2. If \(score\) \(\geqslant\) \(K\), then shrink window by popping out the element from the left until \(score < K\);
  3. Calculate and add \(S\) to the result.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
    long long countSubarrays(vector<int>& nums, long long k) 
    {
        long long sum = 0, ans = 0;
        for(int e = 0, s = 0; e < nums.size(); ++e)
        {           
            sum += nums[e];
            while(sum * (e - s + 1) >= k)
            {
                sum -= nums[s++];
            }

            ans += e - s + 1;
        }

        return ans;
    }
};
Solution time complexity: \(O(n)\)
Solution memory complexity: \(O(1)\)

References