Count Subarrays With Score Less Than K - Problem

Imagine you're analyzing performance metrics for different data segments. 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 calculated as:
(1 + 2 + 3 + 4 + 5) ร— 5 = 15 ร— 5 = 75

Given a positive integer array nums and an integer k, your task is to count how many non-empty subarrays have a score that is strictly less than k.

A subarray is a contiguous sequence of elements within an array. This problem tests your ability to efficiently handle range queries and optimize nested computations.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [2,1,4,3,5], k = 10
โ€บ Output: 6
๐Ÿ’ก Note: Valid subarrays with score < 10 are: [2] (score=2), [1] (score=1), [4] (score=4), [3] (score=3), [5] (score=5), and [2,1] (score=6). Total count = 6.
example_2.py โ€” Larger threshold
$ Input: nums = [1,1,1], k = 5
โ€บ Output: 5
๐Ÿ’ก Note: Valid subarrays: [1] (score=1), [1] (score=1), [1] (score=1), [1,1] (score=4), [1,1] (score=4). Note: [1,1,1] has score=9 which is โ‰ฅ5.
example_3.py โ€” Edge Case
$ Input: nums = [1], k = 2
โ€บ Output: 1
๐Ÿ’ก Note: Only one subarray [1] with score = 1ร—1 = 1 < 2.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 105
  • 1 โ‰ค k โ‰ค 1015
  • All array elements are positive integers

Visualization

Tap to expand
Sliding Window: Count Subarrays with Score < KArray: [2, 1, 4, 3, 5] with k = 102Window: [2]Score: 2ร—1 = 2 < 10 โœ“Count += 1 (subarray [2])14Window: [2, 1]Score: 3ร—2 = 6 < 10 โœ“Count += 2 (subarrays [1], [2,1])3Add 4: [2, 1, 4]Score: 7ร—3 = 21 โ‰ฅ 10 โœ—Need to shrink window!5After shrinking: [4]Score: 4ร—1 = 4 < 10 โœ“Count += 1 (subarray [4])Key Insight:โ€ข For each right position, count all valid subarrays ending thereโ€ข When score โ‰ฅ k, shrink window by moving left pointerโ€ข Each element is processed at most twice (O(n) time)Final Result:Valid subarrays: [2], [1], [2,1], [4], [3], [5] โ†’ Total Count = 6L,R
Understanding the Visualization
1
Initialize Window
Start with both pointers at the beginning, representing day 1 of analysis
2
Expand Window
Add more days to the analysis period, updating running totals
3
Check Performance
Calculate score as (total revenue) ร— (number of days)
4
Shrink if Needed
If performance score exceeds threshold, remove days from the start
5
Count Valid Periods
For each ending day, count how many valid periods end there
Key Takeaway
๐ŸŽฏ Key Insight: The sliding window technique allows us to count all valid subarrays in O(n) time by maintaining a window that expands rightward and shrinks leftward when needed, avoiding the O(nยณ) brute force approach.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
52.3K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen